827 最大人工岛

hard题目,十分困难哈,不是简单的岛屿套路,而是要经历过两次遍历,先一次遍历岛屿再一次遍历海洋

题目

给你一个大小为 n x n 二进制矩阵 grid最多 只能将一格 0 变成 1

返回执行此操作后,grid 中最大的岛屿面积是多少?

岛屿 由一组上、下、左、右四个方向相连的 1 形成。

示例 1:

lua 复制代码
输入: grid = [[1, 0], [0, 1]]
输出: 3
解释: 将一格0变成1,最终连通两个小岛得到面积为 3 的岛屿。

代码与解析

java 复制代码
class Solution {
    int n;
    Map<Integer, Integer> map = new HashMap<>();

    public int largestIsland(int[][] grid) {
        n = grid.length;
        int ans = 0;
        int index = 2;

        // 遍历整个 grid,标记陆地并计算各个岛屿的面积
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    int t = land(grid, i, j, index); // 标记岛屿并计算面积
                    map.put(index, t); // 将岛屿编号和面积存入 map
                    index++;
                    ans = Math.max(ans, t); // 更新最大岛屿面积
                }
            }
        }

        // 处理全海洋情况
        if (ans == 0) return 1;

        // 遍历海洋区域,寻找合并后的最大岛屿面积
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 0) {
                    HashSet<Integer> set = findNeighbor(grid, i, j); // 找到相邻岛屿
                    if (set.size() < 1) continue; // 若无相邻岛屿,则跳过
                    int t = 1;
                    for (Integer m : set) t += map.get(m); // 计算合并后的岛屿面积
                    ans = Math.max(ans, t); // 更新最大岛屿面积
                }
            }
        }
        return ans;
    }

    // 查找相邻的岛屿
    public HashSet<Integer> findNeighbor(int[][] grid, int x, int y) {
        HashSet<Integer> set = new HashSet<>();
        if (!notarea(x - 1, y) && grid[x - 1][y] != 0) set.add(grid[x - 1][y]);
        if (!notarea(x + 1, y) && grid[x + 1][y] != 0) set.add(grid[x + 1][y]);
        if (!notarea(x, y - 1) && grid[x][y - 1] != 0) set.add(grid[x][y - 1]);
        if (!notarea(x, y + 1) && grid[x][y + 1] != 0) set.add(grid[x][y + 1]);
        return set;
    }

    // 标记岛屿并计算面积
    public int land(int[][] grid, int x, int y, int index) {
        if (notarea(x, y) || grid[x][y] == 0) return 0;
        if (grid[x][y] != 1) return 0;
        grid[x][y] = index;
        int region = land(grid, x, y + 1, index) + land(grid, x, y - 1, index)
                + land(grid, x + 1, y, index) + land(grid, x - 1, y, index);
        return region + 1;
    }

    // 判断是否超出边界
    public boolean notarea(int x, int y) {
        return x < 0 || y < 0 || x >= n || y >= n;
    }
}
相关推荐
用户9623779544817 小时前
代码审计 | CC2 链 —— _tfactory 赋值问题 PriorityQueue 新入口
后端
Vfw3VsDKo20 小时前
Maui 实践:Go 接口以类型之名,给 runtime 传递方法参数
开发语言·后端·golang
是真的小外套21 小时前
第十五章:XXE漏洞攻防与其他漏洞全解析
后端·计算机网络·php
ybwycx1 天前
SpringBoot下获取resources目录下文件的常用方法
java·spring boot·后端
小陈工1 天前
Python Web开发入门(十一):RESTful API设计原则与最佳实践——让你的API既优雅又好用
开发语言·前端·人工智能·后端·python·安全·restful
小阳哥AI工具1 天前
Seedance 2.0使用真人参考图生成视频的方法
后端
IeE1QQ3GT1 天前
使用ASP.NET Abstractions增强ASP.NET应用程序的可测试性
后端·asp.net
Full Stack Developme1 天前
SpringBoot多线程池配置
spring boot·后端·firefox
sxhcwgcy1 天前
SpringBoot 使用 spring.profiles.active 来区分不同环境配置
spring boot·后端·spring
稻草猫.1 天前
Spring事务操作全解析
java·数据库·后端·spring