[LeetCode] 827. Making A Large Island 建造一個巨大島嶼



In a 2D grid of 0s and 1s, we change at most one 0 to a 1.html

After, what is the size of the largest island? (An island is a 4-directionally connected group of 1s).java

Example 1:git

Input: [[1, 0], [0, 1]]
Output: 3
Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.

Example 2:github

Input: [[1, 1], [1, 0]]
Output: 4
Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 4.

Example 3:code

Input: [[1, 1], [1, 1]]
Output: 4
Explanation: Can't change any 0 to 1, only one island with area = 4.

Notes:htm

  • 1 <= grid.length = grid[0].length <= 50.
  • 0 <= grid[i][j] <= 1.



這道題在只有0和1的矩陣中用相連的1來表示小島,如今說是有一個把0變爲1的機會,這樣本來不相鄰的島就有可能變的相鄰了,從而組成一個更大的島,讓求出可能組成的最大的島嶼的面積,也就是相連的1的個數。在 LeetCode 中關於島嶼的題其實也作過許多,好比 Number of Distinct Islands IIMax Area of IslandNumber of Distinct IslandsNumber of Islands II,和 Number of Islands。其實大多題目的本質都是用 DFS 或者 BFS 去遍歷全部相連的1,固然這道題也不例外。博主最開始的想法是首先用 DFS 來找出每一個島嶼,而後把同一個島嶼的位置座標都放到同一個 HashSet 中,這樣就有了不少 HashSet,而後遍歷全部的0的位置,對每一個0位置,遍歷其周圍4個鄰居,而後看鄰居位置有沒有屬於島嶼的,有的話就把該島嶼的 HashSet 編號記錄下來,遍歷完4個鄰居後,在把全部的相連的島嶼中的1個數加起來(由於 HashSet 能夠直接求出集合中數字的總個數),每次更新結果 res 便可。這種方法是能夠經過 OJ 的,速度還比下面展現的兩種方法要快,就是代碼比較長,沒有下面方法的簡潔,這裏就不貼了。下面的這兩種方法其實都是從每一個0開始處理,先把0替換成1,而後再用 DFS 來找全部相連的1的個數,具體如何找就跟以前的島嶼的題目沒啥區別了,這裏就不細講了,參見代碼以下:blog



解法一:leetcode

class Solution {
public:
    int largestIsland(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size(), res = 0;
        bool hasZero = false;
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (grid[i][j] == 1) continue;
                grid[i][j] = 1;
                vector<vector<bool>> visited(m, vector<bool>(n));
                res = max(res, helper(grid, i, j, visited));
                if (res == m * n) return res;
                grid[i][j] = 0;
                hasZero = true;
            }
        }
        return hasZero ? res : m * n;
    }
    int helper(vector<vector<int>>& grid, int i, int j, vector<vector<bool>>& visited) {
        int m = grid.size(), n = grid[0].size();
        if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0 || visited[i][j]) return 0;
        visited[i][j] = true;
        return 1 + helper(grid, i - 1, j , visited) + helper(grid, i + 1, j , visited) + helper(grid, i, j - 1, visited) + helper(grid, i, j + 1, visited);
    }
};



固然咱們也能夠用 BFS 來找全部相連的1的個數,整個的解題思路和上面的解法並無什麼不一樣,並不難理解,這可能就是論壇上會有人質疑這道題不該該標爲 Hard 的緣由,參見代碼以下:get



解法二:同步

class Solution {
public:
    int largestIsland(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size(), res = 0;
        bool hasZero = false;
        vector<int> dirX{0, -1, 0, 1}, dirY{-1, 0, 1, 0};
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (grid[i][j] == 1) continue;
                vector<vector<bool>> visited(m, vector<bool>(n));
                queue<int> q{{i * n + j}};
                int sum = 0;
                while (!q.empty()) {
                    int t = q.front(); q.pop();
                    ++sum;
                    for (int k = 0; k < 4; ++k) {
                        int x = t / n + dirX[k], y = t % n + dirY[k];
                        if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == 0 || visited[x][y]) continue;
                        visited[x][y] = true;
                        q.push(x * n + y);
                    }
                }
                res = max(res, sum);
                if (res == m * n) return res;
                hasZero = true;
            }
        }
        return hasZero ? res : m * n;
    }
};



Github 同步地址:

https://github.com/grandyang/leetcode/issues/827



相似題目:

Number of Distinct Islands II

Max Area of Island

Number of Distinct Islands

Number of Islands II

Number of Islands



參考資料:

https://leetcode.com/problems/making-a-large-island/

https://leetcode.com/problems/making-a-large-island/discuss/313787/Two-java-solutions

https://leetcode.com/problems/making-a-large-island/discuss/127256/DFS-JAVA-AC-CONCISE-SOLUTION

https://leetcode.com/problems/making-a-large-island/discuss/127015/C%2B%2B-O(n*m)-15-ms-colorful-islands



LeetCode All in One 題目講解彙總(持續更新中...)

相關文章
相關標籤/搜索