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的更多相关文章

  1. Go1.7改善了编译速度并且会生成更快的代码

    Go1.7的开发周期正在接近它的下一个里程碑,Go的提交者Dave Cheney报告了子即将发布的版本中,团队成员在语言工具链上的努力. Cheney称,基于当前的开发状态,Go1.7将会很容易就成为 ...

  2. 梭子鱼:APT攻击是一盘更大的棋吗?

    随着企业对IT的依赖越来越强,APT攻击可能会成为一种恶意打击竞争对手的手段.目前,APT攻击目标主要有政治和经济目的两大类.而出于经济目的而进行的APT攻击可以获取竞争对手的商业信息,也可使用竞争对 ...

  3. Qt带来的是更加低廉的开发成本和学习成本,对于很多小公司而言,这种优势足以让他们获得更大的利润空间 good

    不能单纯从技术上来看待这个问题,Qt本来是小众的开发平台,个人认为,它的出现只是解决特性场景的特定问题,Qt带来的是更加低廉的开发成本和学习成本,对于很多小公司而言,这种优势足以让他们获得更大的利润空 ...

  4. 在.NET中快速创建一个5GB、10GB或更大的空文件

    对于通过UDP进行打文件传输的朋友应该首先会考虑到一个问题,那就是由于UDP并不会根据先来先到原则进行发送,也许你发送端发送的时候是以包1和包2的顺序传输的,但接收端可能以包2和包1 的顺序来进行接收 ...

  5. [Swift]LeetCode496. 下一个更大元素 I | Next Greater Element I

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  6. [Swift]LeetCode503. 下一个更大元素 II | Next Greater Element II

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  7. [Swift]LeetCode1019. 链表中的下一个更大节点 | Next Greater Node In Linked List

    We are given a linked list with head as the first node.  Let's number the nodes in the list: node_1, ...

  8. 1197多行事务要求更大的max_binlog_cache_size处理与优化

    1197多语句事务要求更大的max_binlog_cache_size报错   binlog_cache_size:为每个session 分配的内存,在事务过程中用来存储二进制日志的缓存,提高记录bi ...

  9. MT【272】更大的视野,更好的思路.

    已知$f(x)=\sum\limits_{k=1}^{2017}\dfrac{\cos kx}{\cos^k x},$则$f(\dfrac{\pi}{2018})=$_____ 分析:设$g(x)=\ ...

随机推荐

  1. kali linux 64bit 2019.1a下启动bbqsql:No module named coros

    kali linux 64bit 2019.1a下bbqsql启动失败,错误: File "/usr/local/lib/python2.7/dist-packages/bbqsql/lib ...

  2. 学习Django,http协议,

    学习Django http协议 规则 ''' http协议:超文本传输协议 ①基于TCP/IP协议基础上的应用层协议,底层实现仍为socket ②基于请求-响应模式:通讯一定是从客户端开始,服务器端接 ...

  3. C# 尝试读取或写入受保护的内存。这通常指示其他内存已损坏

    用管理员身份运行CMD,输入netsh winsock reset并回车(注意,必须是已管理员身份运行,这个重置LSP连接)运行后提示要重启生效,结果没重启就OK了(重启不重启看最终效果).

  4. ununtu 18.04 163 mirror

    deb http://mirrors.163.com/ubuntu/ bionic main restricted deb http://mirrors.163.com/ubuntu/ bionic- ...

  5. shell 编程中的 知识点 - 突然一下子就明白很多东西了

    按自己的意愿生活, 而且是::: 要敢于按自己的意志去活! 那是一种胆量和勇气!! shell中的结构, 只有选择(实际上if条件也是一种选择结构)和循环, 都是用关键字来替代 大括号的. 如: if ...

  6. RocketMQ3.2.2生产者发送消息自动创建Topic队列数无法超过4个

    问题现象 RocketMQ3.2.2版本,测试时尝试发送消息时自动创建Topic,设置了队列数量为8: producer.setDefaultTopicQueueNums(8); 同时设置broker ...

  7. Module controller in JMeter

    https://qualibrate.com/blog/quality-assurance/jmeter-module-controller/ 通过组合Test Fragments 和Module C ...

  8. (zhuan) 资源|TensorFlow初学者必须了解的55个经典案例

    资源|TensorFlow初学者必须了解的55个经典案例 2017-05-27 全球人工智能 >>>>>>欢迎投稿:news@top25.cn<<< ...

  9. 文件IO(2)

    Lseek:       ***************************************************************************    实验一:     ...

  10. 通过sql语句修改表的结构

    1.修改表的列名 oracle: ALTER TABLE 表名 RENAME COLUMN 列名 TO 新列名sqlserver:exec sp_rename '[表名].[列名]','[表名].[新 ...