leetcode 827 最大人工岛

简介: leetcode 827 最大人工岛

最大人工岛


8913e6b71d124d7385459fbb7bff9238.png

class Solution {
public:
    int m = 0 , n = 0;
    int dir[4][2] = {0,-1,0,1,-1,0,1,0};
    int tmp_sum = 1 , bolck_num = 1;
    void dfs(vector<vector<int>>& grid ,vector<vector<bool>> &path , int x ,int y ,int num )
    {
        for(int i=0 ; i<4 ;i++)
        {
            int next_x = x + dir[i][0];
            int next_y = y + dir[i][1];
            if(next_x<0||next_x>=m||next_y<0||next_y>=n) continue;
            if(grid[next_x][next_y] == 1 && path[next_x][next_y] == false)
            {
                tmp_sum++;
                grid[next_x][next_y] = num;
                dfs(grid,path,next_x,next_y,num);
            }
        }
    }
    int largestIsland(vector<vector<int>>& grid) {
        m = grid.size();
        n = grid[0].size();
        vector<vector<bool>> path(m,vector<bool>(n,false));
        map<int,int> my_map;
        for(int i=0 ; i<m ;i++)
        {
            for(int j=0 ; j<n ;j++)
            {
                if(grid[i][j] == 1 && path[i][j] == false)
                {
                    bolck_num++;
                    path[i][j] = true;
                    grid[i][j] = bolck_num;
                    tmp_sum=1;
                    dfs(grid,path,i,j,bolck_num);
                    my_map[bolck_num] = tmp_sum;
                }
            }
        }
        int result = 0 , tmp_result = 1;
        for(int i=0 ; i<m ;i++)
        {
            for(int j=0 ; j<n ;j++)
            {
                if(grid[i][j] == 0 && path[i][j] == false)
                {
                    path[i][j] = true;
                    tmp_result = 1;
                    set<int> my_set;
                    for(int k=0 ; k<4 ;k++)
                    {
                        int next_x = i + dir[k][0];
                        int next_y = j + dir[k][1];
                        if(next_x<0||next_x>=m||next_y<0||next_y>=n) continue;
                        if(grid[next_x][next_y] != 0 ) my_set.insert(grid[next_x][next_y]);
                    }
                    for(auto it = my_set.begin() ; it!=my_set.end();it++) tmp_result += my_map[*it];
                    my_set.clear();
                    if(tmp_result > result) result = tmp_result;
                }
            }
        }
        if(result == 0) return m*n;
        return result;
    }
};
相关文章
|
11月前
[leetcode] 827. 最大人工岛 | 二维并查集
[leetcode] 827. 最大人工岛 | 二维并查集
52 0
|
12月前
|
算法 C++
每日算法系列【LeetCode 827】最大人工岛
每日算法系列【LeetCode 827】最大人工岛
|
6天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
9 0
|
6天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
13 0
|
6天前
|
算法
【刷题】 leetcode 面试题 08.05.递归乘法
递归算法是一种在计算机科学和数学中广泛应用的解决问题的方法,其基本思想是利用问题的自我相似性,即将一个大问题分解为一个或多个相同或相似的小问题来解决。递归算法的核心在于函数(或过程)能够直接或间接地调用自身来求解问题的不同部分,直到达到基本情况(也称为基础案例或终止条件),这时可以直接得出答案而不必再进行递归调用。
25 4
【刷题】 leetcode 面试题 08.05.递归乘法
|
6天前
|
存储 算法 安全
【刷题】 leetcode 面试题 01.06 字符串压缩
来看效果: 非常好!!!过啦!!!
27 5
【刷题】 leetcode 面试题 01.06 字符串压缩
|
6天前
|
存储 算法 测试技术
|
6天前
|
算法 C语言 C++
|
6天前
|
存储 算法 C语言
C语言刷题~Leetcode与牛客网简单题
C语言刷题~Leetcode与牛客网简单题