ඔස්ට්‍රෙලියාවෙ යක්ක පුතුන්ට බනිනවා

saw1993

Well-known member
  • Aug 9, 2015
    1,709
    1,716
    113
    Kandy
    දැන් ඔය රටවල් වලට ගිය උන්ට එහෙ පුරවැසිකම නැද්ද?? තාම ශ්‍රීලාංකික කියල කියන්නේ මොකක් හරි උනාම :rofl:
    ලාංකීය සම්බවයක් සහිත කියලා නේ කියන්නෙ
     

    RealityOfX

    Well-known member
  • Feb 5, 2021
    8,895
    1
    15,236
    113
    ලාංකීය සම්බවයක් සහිත කියලා නේ කියන්නෙ
    කොහෙත් එකයි ජාතිය ඉස්සරහට දාල නිව්ස් කියන්නෙ මොකක්හරි නරක දෙයක් උනාම
    me amazon neme, koi kaleda interview tibbe?
    අවුරුදු එකාමාරකට විතර කලින්
    .
     

    EKGuest

    Well-known member
  • Nov 16, 2022
    3,206
    5,704
    113
    oya pahala 2d array eke 1 kiyanne lands, 0 kiyanne water. wadima connected lands gana hoyanna function ekak liyanna. Me magen ahapu question ekak. connected wenna puluwan. top, bottom, left, right vitrai. me wage angles walata connect wenna ba. / \


    int[][] grid =
    {{0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
    {0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0},
    {0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}};

    public int maxAreaOfIsland(int[][] grid) {}

    oya FAANG company ekaka magen ahapu question ekak, ehema wate gihin athal ganna ba ban, mewa un train wenna krna interview neme.10 mins wage denne oya wage ekak solve krna.

    මේක විනාඩි 10 න් හැදුවානම් ඔයා expert කෙනෙක්. මට මේක හදන්න පැයක් විතර ගියා. Flood Fill Algorithm එක use කලේ. Solution එක පහලින් දානවා හරිද කියලා බලන්න.

    C#:
            public static int MaxAreaOfIsland(int[,] grid)
            {
                int maxArea = 0;
    
                int height = grid.GetLength(0);
                int width = grid.GetLength(1);
                bool[,] visited = new bool[height, width];
    
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        if (grid[i, j] == 1 && !visited[i, j])
                        {
                            int area = FindArea(i, j, grid, height, width, visited);
                            if (area > maxArea)
                                maxArea = area;
                        }
                    }
                }
    
                return maxArea;
            }
    
            static int FindArea(int rowStart, int colStart, int[,] grid, int height, int width, bool[,] visited)
            {
                List<Tuple<int, int>> queue = new List<Tuple<int, int>>();
    
                queue.Add(new Tuple<int, int>(rowStart, colStart));
                visited[rowStart, colStart] = true;
    
                int area = 0;
    
                while (queue.Count > 0)
                {
                    int row = queue[0].Item1;
                    int col = queue[0].Item2;
                    area++;
                    queue.RemoveAt(0);
                    if (row + 1 < height && grid[row + 1, col] == 1 && !visited[row + 1, col])
                    {
                        queue.Add(new Tuple<int, int>(row + 1, col));
                        visited[row + 1, col] = true;
                    }
                    if (col + 1 < width && grid[row, col + 1] == 1 && !visited[row, col + 1])
                    {
                        queue.Add(new Tuple<int, int>(row, col + 1));
                        visited[row, col + 1] = true;
                    }
                    if (row - 1 >= 0 && grid[row - 1, col] == 1 && !visited[row - 1, col])
                    {
                        queue.Add(new Tuple<int, int>(row - 1, col));
                        visited[row - 1, col] = true;
                    }
                    if (col - 1 >= 0 && grid[row, col - 1] == 1 && !visited[row, col - 1])
                    {
                        queue.Add(new Tuple<int, int>(row, col - 1));
                        visited[row, col - 1] = true;
                    }
                }
    
                return area;
            }

    Output:

    image.png


    Complete solution link: https://dotnetfiddle.net/PpGeXq