[LeetCode] 827. Making A Large Island
In a 2D grid of 0
s and 1
s, we change at most one 0
to a 1
.
After, what is the size of the largest island? (An island is a 4-directionally connected group of 1
s).
Example 1:
Input: [[1, 0], [0, 1]]
Output: 3
Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
Example 2:
Input: [[1, 1], [1, 0]]
Output: 4
Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 4.
Example 3:
Input: [[1, 1], [1, 1]]
Output: 4
Explanation: Can't change any 0 to 1, only one island with area = 4.
Notes:
1 <= grid.length = grid[0].length <= 50
.0 <= grid[i][j] <= 1
.
这个题目我只能想到naive的做法, T: O((m*n)^2), 就是对每个 grid[i][j] == 0 的元素, 将其换为1, 然后用BFS来计算size, 一直循环, 找最大值, 如果没有0 的话返回m*n.
然后参考了这个T: O(m*n) 的做法. 思路就是最开始将grid扫一遍, 每次如果grid[i][j] == 1, 将该点及所有跟它相连的为1 的点, 标记为color, 然后将各个不同的island tag为不同颜色, 并且用d2来存color和这个color的island 的size, 思路和做法跟[LeetCode] 200. Number of Islands_ Medium tag: BFS一样, 只是需要标记color.
这样之后可以得到如上图所示的结果, 然后再将grid扫一遍, 这一次是针对 grid[i][j] == 0 的元素, 然后看四个邻居是否为1, 如果是的话将它的size加入到temp里面,只是要注意的是有可能同样颜色的可以是多个neig, 所以要用set来排除已经加入同样颜色的size了, 最后返回最大值即可.
1. Constraints
1) size of grid, [1*1] ~ [50*50]
2) element will only be 0 or 1
2. Ideas
DFS/BFS 用BFS, T: O(m*n) S: O(m*n)
3. Code
class Solution:
def largestIsland(self, grid):
def bfs(grid, d1, i, j, d2, dirs, color):
lr, lc, d1[(i, j)], queue, size = len(grid), len(grid[0]), color, collections.deque([(i,j)]), 1 while queue:
pr, pc = queue.popleft()
for c1, c2 in dirs:
nr, nc = c1 + pr , c2 + pc
if 0 <= nr < lr and 0 <= nc < lc and (nr, nc) not in d1:
d1[(nr, nc)] = color
queue.append((nr, nc))
size += 1
d2[color] = size
d1, d2, lr, lc , dirs, temp, color = {},{}, len(grid), len(grid[0]), [(0,1), (0,-1), (1, 0), (-1,0)], -1, 1
for i in range(lr):
for j in range(lc):
if grid[i][j] == 1 and (i,j) not in d1:
color += 1
bfs(grid, d1, i, j, d2, dirs, color) for i in range(lr):
for j in range(lc):
if grid[i][j] == 0:
colors = set()
for c1, c2 in dirs:
nr, nc = i+ c1, j + c2
if 0 <= nr < lr and 0 <= nc < lc and grid[nr][nc] == 1:
colors.add(d1[nr][nc])
s = sum(d2[color] for color in colors) + 1
temp = s if temp == -1 else max(temp, s)
return temp if temp != -1 else lr*lc
2)
class Solution(object):
def largestIsland(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
lr, lc, dirs, colorMap, ans, colorSize, queue, curColor = len(grid), len(grid[0]), [(1, 0), (-1, 0), (0, 1), (0, -1)], {}, 0, {}, collections.deque(), 0
# color all the islands and save the pos -> color in colorMap, color -> size in colorSize
for i in range(lr):
for j in range(lc):
if grid[i][j] == 1 and (i, j) not in colorMap:
size = 0
curColor += 1
queue.append((i, j))
colorMap[(i, j)] = curColor
while queue:
r, c = queue.popleft()
size += 1
for d1, d2 in dirs:
nr, nc = r + d1, c + d2
if 0 <= nr < lr and 0 <= nc < lc and (nr, nc) not in colorMap and grid[nr][nc] == 1:
queue.append((nr, nc))
colorMap[(nr, nc)] = curColor
colorSize[curColor] = size
ans = max(ans, size) #### to each i, j that grid[i][j] == 0 , put all neigbours that is 1 and add all the color in a set, sum all color sizes and max(ans, sizeSum)
for i in range(lr):
for j in range(lc):
if grid[i][j] == 0:
colorSet = set()
for d1, d2 in dirs:
nr, nc = i + d1, j + d2
if 0 <= nr < lr and 0 <= nc < lc and grid[nr][nc] == 1:
colorSet.add(colorMap[(nr, nc)])
ans = max(ans, 1 + sum(colorSize[color] for color in colorSet))
return ans
[LeetCode] 827. Making A Large Island的更多相关文章
- [LeetCode] 827. Making A Large Island 建造一个巨大岛屿
In a 2D grid of 0s and 1s, we change at most one 0 to a 1. After, what is the size of the largest is ...
- 【leetcode】827. Making A Large Island
题目如下: 解题思路:这个题目可以进行拆分成几个子问题.第一,求出island的数量,其实就是 200. Number of Islands,这个很简单,DFS或者BFS都能搞定:第二,除了求出isl ...
- LeetCode 695. Max Area of Island (岛的最大区域)
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- [Swift]LeetCode827. 最大人工岛 | Making A Large Island
In a 2D grid of 0s and 1s, we change at most one 0 to a 1. After, what is the size of the largest is ...
- [Leetcode]695. Max Area of Island
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- [LeetCode] 830. Positions of Large Groups_Easy tag: Two Pointers
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- 【leetcode】Max Area of Island
国庆中秋长假过完,又要开始上班啦.先刷个题目找找工作状态. Given a non-empty 2D array grid of 0's and 1's, an island is a group o ...
- Java实现 LeetCode 827 最大人工岛(DFS+暴力模拟)
827. 最大人工岛 在二维地图上, 0代表海洋, 1代表陆地,我们最多只能将一格 0 海洋变成 1变成陆地. 进行填海之后,地图上最大的岛屿面积是多少?(上.下.左.右四个方向相连的 1 可形成岛屿 ...
- [Leetcode]827.使用回溯+标记解决最大人工岛问题
在二维地图上, 0代表海洋, 1代表陆地,我们最多只能将一格 0 海洋变成 1变成陆地. 进行填海之后,地图上最大的岛屿面积是多少?(上.下.左.右四个方向相连的 1 可形成岛屿) 示例 1: 输入: ...
随机推荐
- 【2014年12月6日】HR交流会
今天的交流会感觉还是不错,体会到了一些东西,把它记下来. 想到什么写什么,可能没什么条理. 1.先选行业,再选职业,再选公司 根据自己的兴趣以及个人特长,能力等方面,需要定一个大概的方向,然后根据方向 ...
- Entity Framework6的在线下载安装
Entity Framework6的在线下载安装 Entity Framework 简单介绍: 看名字就知道肯定是关于数据模型的…… Entity Framework:微软官方提供的ORM()工具,O ...
- 如何分离p12(或pfx)文件中的证书和私钥
p12(或者pfx)文件里一般存放有CA的根证书,用户证书和用户的私钥 假设我们有一个test.p12文件 在安装了openssl的linux服务器上执行以下命令: 提取用户证书: openssl p ...
- 【CF888G】Xor-MST Trie树(模拟最小生成树)
[CF888G]Xor-MST 题意:给你一张n个点的完全图,每个点有一个权值ai,i到j的边权使ai^aj,求这张图的最小生成树. n<=200000,ai<2^30 题解:学到了求最小 ...
- node项目部署相关问题
process.env process.env属性返回一个对象,包含了当前Shell的所有环境变量. 通常的做法是,新建一个环境变量NODE_ENV,用它确定当前所处的开发阶段,生产阶段设为produ ...
- 9.4Django
2018-9-4 17:19:13 昨天和基友玩到了十点 一路溜着玩喝豆腐脑,谈谈人生!感触颇深! 越努力,越幸运! 关于 Django的一开始的配置东西! 2018-9-4 19:42:27 201 ...
- Docker 学习应用篇之三: Docker的简单实用
安装完Docker之后,我们就可以简单的使用Docker,来体会Docker的用处. 首先看下Docker的常用命令,都是我在实用Docker的时候用到的命令: docker常用命令: $ docke ...
- ASP.NET Web配置指南
利用ASP.NET,可以指定影响服务器上所有的Web应用程序.仅影响单个的应用程序.影响个别页面.或影响Web应用程序中的个别文件夹的配置设置.可以对编译器选项.调试.用户身份验证.错误消息显示.连接 ...
- 2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]
题目链接:https://www.nowcoder.com/acm/contest/143/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
- Linux/Unix命令行安装weblogic软件
--通过java -jar wls1036_generic.jar启动安装weblogic软件进程: [weblogic@localhost mnt]$ java -jar wls1036_gener ...