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. spring boot 结合Redis 实现工具类

    自己整理了 spring boot 结合 Redis 的工具类引入依赖 <dependency> <groupId>org.springframework.boot</g ...

  2. 20145208 蔡野 《网络对抗》Exp7 网络欺诈技术防范

    20145208 蔡野 <网络对抗>Exp7 网络欺诈技术防范 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法.具体有(1)简单应用SET工具建立冒名网站(2) ...

  3. C# 文件与二进制之间的转换

    /// <summary> /// 工具类:文件与二进制流间的转换 /// </summary> public class FileBinaryConvertHelper { ...

  4. Kibana --> Getting Started -->Building your own dashboard

    https://www.elastic.co/guide/en/kibana/6.6/tutorial-build-dashboard.html Building your own dashboard ...

  5. Python之Requests的高级用法

    # 高级用法 本篇文档涵盖了Requests的一些更加高级的特性. ## 会话对象 会话对象让你能够跨请求保持某些参数.它也会在同一个Session实例发出的所有请求之间保持cookies. 会话对象 ...

  6. oracle单行函数 之 字符函数

    Upper(字符串 / 列):将输入的字符串变成大写 Lower(字符串 / 列):将输入的字符串变成小写 Initcap(字符串 / 列):开头首字母大写 Length(字符串 / 列):字符串长度 ...

  7. C# 里调用vb的inputbox弹出窗

    https://blog.csdn.net/hutao1101175783/article/details/16800871 先对项目添加对Microsoft.VisualBasic的引用 Inter ...

  8. 4-Four-Seeing hands

      ①Several cases have been reported in Russia recently of people who can read and detect colours wit ...

  9. 常用Iview样式布局

    type 布局模式,可选值为flex流式布局或不选,在现代浏览器下有效 flex 布局下的垂直对齐方式,align可选值为top.middle.bottom flex 布局下的水平排列方式,justi ...

  10. Jenkins-job迁移

    摘自:http://www.cnblogs.com/topplay/p/3899330.html Jenkins迁移job 说明:从一个Jenkins服务器A将现有job迁移到另外一个Jenkins服 ...