生成更大的陆地 Making A Large Island
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的更多相关文章
- Go1.7改善了编译速度并且会生成更快的代码
Go1.7的开发周期正在接近它的下一个里程碑,Go的提交者Dave Cheney报告了子即将发布的版本中,团队成员在语言工具链上的努力. Cheney称,基于当前的开发状态,Go1.7将会很容易就成为 ...
- 梭子鱼:APT攻击是一盘更大的棋吗?
随着企业对IT的依赖越来越强,APT攻击可能会成为一种恶意打击竞争对手的手段.目前,APT攻击目标主要有政治和经济目的两大类.而出于经济目的而进行的APT攻击可以获取竞争对手的商业信息,也可使用竞争对 ...
- Qt带来的是更加低廉的开发成本和学习成本,对于很多小公司而言,这种优势足以让他们获得更大的利润空间 good
不能单纯从技术上来看待这个问题,Qt本来是小众的开发平台,个人认为,它的出现只是解决特性场景的特定问题,Qt带来的是更加低廉的开发成本和学习成本,对于很多小公司而言,这种优势足以让他们获得更大的利润空 ...
- 在.NET中快速创建一个5GB、10GB或更大的空文件
对于通过UDP进行打文件传输的朋友应该首先会考虑到一个问题,那就是由于UDP并不会根据先来先到原则进行发送,也许你发送端发送的时候是以包1和包2的顺序传输的,但接收端可能以包2和包1 的顺序来进行接收 ...
- [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 ...
- [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 ...
- [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, ...
- 1197多行事务要求更大的max_binlog_cache_size处理与优化
1197多语句事务要求更大的max_binlog_cache_size报错 binlog_cache_size:为每个session 分配的内存,在事务过程中用来存储二进制日志的缓存,提高记录bi ...
- MT【272】更大的视野,更好的思路.
已知$f(x)=\sum\limits_{k=1}^{2017}\dfrac{\cos kx}{\cos^k x},$则$f(\dfrac{\pi}{2018})=$_____ 分析:设$g(x)=\ ...
随机推荐
- java不常用但很有用的问题排查工具(持续完善)
因为用的频率不是很多,老忘掉,每次都要搜下,特记录下备忘. 查看进程的启动jvm选项 [root@iZ23nn1p4mjZ ~]# jinfo -flags 16603Attaching to pro ...
- 01:adminLTE2基本使用
1.1 adminLTE介绍 1.adminLTE 介绍 1.基于Bootstrap3高度可定制的响应式管理模板,免去前端架构师大量的js和css的编写 2.adminLTE除了可以使用bootstr ...
- collectd 检测cpu使用率
环境配置 1. install epel https://www.cyberciti.biz/faq/installing-rhel-epel-repo-on-centos-redhat-7-x/ 2 ...
- js之 data-*自定义属性
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- java常用类-StringBuffer,Integer,Character
* StringBuffer: * 线程安全的可变字符串. * * StringBuffer和String的区别? * 前者长度和内容可变,后者不可变. * 如果使用前者做字符串的拼接,不会浪费太多的 ...
- 0x17二叉堆之超市
题目链接:https://www.acwing.com/problem/content/147/ 容易想到一个贪心策略:在最优解中,对于每个时间(天数) t,应该在保证不卖出过期商品的前提下,尽量卖出 ...
- 【python51--__name__属性】
一.基础知识 1.__name__ == '__main__' 所有模块都有一个__name__属性,__name__的值取决于如何应用模块,在作为独立程序运行的时候,__name__属性的值是‘__ ...
- topcoder srm 465 div1
problem1 link 以两个点$p,q$为中心的两个正方形的边长和最大为$2dist(p,q)$,即$p,q$距离的两倍. 也就是两个$p,q$的连线垂直穿过两个正方形的一对边且平分两个正方形. ...
- C# 反射小结
废话不多说,直接上代码. 1.typeof(类名):它是一个运算符 eg_1:Type type = typeof(int) ; eg_2:public class Student { Type ty ...
- 【无法使用yum安装软件】使用yum命令安装软件提示No package numactl.x86_64 available.
在安装mysql时需要安装numactl.x86_64 使用yum -y install numactl.x86_64时报错 [root@sdp6 mysql]# yum -y install num ...