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. TensorFlow 生成 .ckpt 和 .pb

    原文:https://www.cnblogs.com/nowornever-L/p/6991295.html 1. TensorFlow  生成的  .ckpt 和  .pb 都有什么用? The . ...

  2. Received empty response from Zabbix Agent at [172.16.1.7]...

    Centos7.5  zabbix添加主机发现ZBX爆红报错 原因:在配置/etc/zabbix/zabbix_agentd.conf中172.16.1.71写成了127.16.1.71 解决方法:重 ...

  3. FJUT3565 最大公约数之和(容斥)题解

    题意:给n,m,求出 思路:题意为求出1~m所有数和n的gcd之和.显然gcd为n的因数.我们都知道gcd(a,b)= c,那么gcd(a/c,b/c)= 1.也就是说我们枚举n所有的因数k,然后去找 ...

  4. Golang初练手-多线程网站路径爆破

    以前用Python写过这个工具,前两天看了golang的基础,就想着用这个语言把这个工具重写一遍 先放张图 用法 Example : Buster.exe -u=https://www.baidu.c ...

  5. Asp.net 之 window 操作命令

    命令:cmd  打开执行窗口 命令:inetmgr.打开iis管理器 命令:dcomcnfg 打开组件服务 命令:regedit   打开注册表

  6. C# 各种控件实现可拖动和调整大小

    http://www.360doc.com/content/18/0516/12/55659281_754382494.shtml using System; using System.Collect ...

  7. .psl脚本介绍

    .ps1文件是PowerShell写好的脚本文件 可以在记事本中写一段PowerShell代码,然后将其保存为“xxx.ps1”,后面要使用它的时候,双击即可运行了.这有点像批处理的“.bat”文件, ...

  8. 洛谷P2782 友好城市 DP

    やはり まだあしたということは嘘でしょう.ぜんぶ忘れた( ´・ヮ・`) 所以今天就贴一道水题吧 原题>>https://www.luogu.org/problem/show?pid=278 ...

  9. Wijmo 2017 V1发布

    2017年Wijmo的第1个Release已经发布了!它充满了令人兴奋的新控件和新功能.一个新的TreeView控件:一个只有看到你才会相信的MultiAutoComplete控件:移动平台报表查看器 ...

  10. 1:httpd-2.2基础

    在配置httpd主配置文件时,应该先记得备份一下: #cd /etc/httpd/conf/ #cp httpd.conf{,.bak} #vim /etc/httpd/conf/httpd.conf ...