產生更大的陸地 Making A Large Island

來源:互聯網
上載者:User

標籤:new   連通   ++   min   r++   分享圖片   private   vat   max   

2018-10-06 19:44:18

問題描述:

問題求解:

經典的求連通塊問題的擴充,問題規模不大,可以暴力求解。

解法一、Brute Force O(n^4)

    int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};    public int largestIsland(int[][] grid) {        int res = Integer.MIN_VALUE;        int n =  grid.length;        for (int i = 0; i < n; i++) {            for (int j = 0; j < n; j++) {                if (grid[i][j] == 1) res = Math.max(res, helper(grid, i, j, new int[n][n]));                else {                    grid[i][j] = 1;                    res = Math.max(res, helper(grid, i, j, new int[n][n]));                    grid[i][j] = 0;                }            }        }        return res;    }    private int helper(int[][] grid, int x, int y, int[][] used) {        int n = grid.length;        int res = 1;        used[x][y] = 1;        for (int[] dir : dirs) {            int px = x + dir[0];            int py = y + dir[1];            if (px < 0 || px >= n || py < 0 || py >= n || used[px][py] == 1 || grid[px][py] == 0) continue;            res += helper(grid, px, py, used);        }        return res;    }

解法二、

為每個連通塊做上標記,並得到每個連通塊的面積,之後再對0進行遍曆,依次尋找其四個相鄰的邊的area,將他們加起來再從中取max。演算法總的時間複雜度為O(n ^ 2)。

    public int largestIsland(int[][] grid) {        int res = Integer.MIN_VALUE;        int n =  grid.length;        Map<Integer, Integer> map = new HashMap<>();        int color = 0;        int area = 0;        map.put(color++, area);        for (int i = 0; i < n; i++) {            for (int j = 0; j < n; j++) {                if (grid[i][j] == 1) area = dfs(grid, i, j, ++color);                map.put(color, area);                res = Math.max(res, area);            }        }        for (int i = 0; i < n; i++) {            for (int j = 0; j < n; j++) {                if (grid[i][j] == 0) {                    int curArea = 1;                    Set<Integer> set = new HashSet<>();                    set.add(getColor(grid, i - 1, j));                    set.add(getColor(grid, i + 1, j));                    set.add(getColor(grid, i, j + 1));                    set.add(getColor(grid, i, j - 1));                    for (int c : set) {                        curArea += map.get(c);                    }                    res = Math.max(res, curArea);                }            }        }        return res;    }    private int getColor(int[][] grid, int x, int y) {        if (x < 0 || x >= grid.length || y < 0 || y >= grid.length) return 0;        else return grid[x][y];    }    private int dfs(int[][] grid, int x, int y, int color) {        if (x < 0 || x >= grid.length || y < 0 || y >= grid.length || grid[x][y] != 1) return 0;        grid[x][y] = color;        return 1 + dfs(grid, x + 1, y, color) + dfs(grid, x - 1, y, color) +                dfs(grid, x, y - 1, color) + dfs(grid, x, y + 1, color);    }

 

產生更大的陸地 Making A Large Island

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.