生成更大的陆地 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)=\ ...
随机推荐
- shell脚本一键安装redis集群
简介: 明天再写,上脚本 #!/bin/bash #-------------------------------------------------------------------------- ...
- [C++ Primer Plus] 第2章、开始学习c++
一.程序清单2.1(代码和书略不一样) #include<iostream> using namespace std;//使用std这个命名空间,才能正确找到cin和cout,如果不使用命 ...
- Codeforces 888G Xor-MST - 分治 - 贪心 - Trie
题目传送门 这是一条通往vjudge的高速公路 这是一条通往Codeforces的高速公路 题目大意 给定一个$n$阶完全图,每个点有一个权值$a_{i}$,边$(i, j)$的权值是$(a_{i}\ ...
- 【python--函数解读】
1.strip()函数:用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列(包括'\n', '\r', '\t', ' ') 注意: 该方法只能删除开头或是结尾的字符,不能删除中间部分的 ...
- matlab做聚类分析
说明:如果是要用matlab做kmeans聚类分析,直接使用函数kmeans即可.使用方法:kmeans(输入矩阵,分类个数k). 转载一: MATLAB提供了两种方法进行聚类分析: 1.利用 clu ...
- 洛谷luogu2782
P2782 友好城市 题目描述 有一条横贯东西的大河,河有笔直的南北两岸,岸上各有位置各不相同的N个城市.北岸的每个城市有且仅有一个友好城市在南岸,而且不同城市的友好城市不相同.每对友好城市都向政府申 ...
- 【做题】agc003E - Sequential operations on Sequence——经典结论
题意:有一个序列,初始是从\(1\)到\(n\)的\(n\)个数.有\(q\)次操作,每次操作给出\(q_i\),把当前的序列重复无数遍,然后截取最前面的\(q_i\)个元素作为新序列.要求输出完成所 ...
- 分布式事务框架&解决方案参考
两种开源解决方案框架介绍: https://blog.csdn.net/zyndev/article/details/79604395#_97 LCN: https://www.jianshu.com ...
- What are the differences between Flyweight and Object Pool patterns?
What are the differences between Flyweight and Object Pool patterns? They differ in the way they are ...
- (转)Autonomous_Vehicle_Paper_Reading_List
Autonomous_Vehicle_Paper_Reading_List 2018-07-19 10:40:08 Reference:https://github.com/ZRZheng/Auton ...