[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: 输入: ...
随机推荐
- css案例 - 评分效果的星星✨外衣
纳尼?什么星星外衣?好,直接上图比较能说清楚: 仔细看会发现规律:可以根据百分比/分值动态改变高亮星星的个数. 分步骤图: 这种效果,如果遇到一分一个星,没有半星(或者有也可以,直接加一个半星的类名) ...
- cookie设置在特定时间点过期的方法
假设需求为:在当天晚上0:00过期. 方法: 得到当天晚上0:00这个时间点的一个时间. function getNextDate(){ var d = new Date(), ...
- 傲游浏览器---自定义 UserAgent 字符串
遨游浏览器:http://www.maxthon.cn/ 自定义 UserAgent : http://www.fynas.com/ua 手机UserAgent大全 设备 系统 浏览器 User-A ...
- C语言如何产生随机数
1.基本函数 在C语言中取随机数所需要的函数是: int rand(void); void srand(unsigned int n); rand()函数和srand()函数被声明在头文件stdlib ...
- 【POJ2409】Let it Bead Pólya定理
[POJ2409]Let it Bead 题意:用$m$种颜色去染$n$个点的环,如果两个环在旋转或翻转后是相同的,则称这两个环是同构的.求不同构的环的个数. $n,m$很小就是了. 题解:在旋转$i ...
- 【LOJ6254】最优卡组 堆(模拟搜索)
[LOJ6254]最优卡组 题面 题解:常用的用堆模拟搜索套路(当然也可以二分).先将每个卡包里的卡从大到小排序,然后将所有卡包按(最大值-次大值)从小到大排序,并提前处理掉只有一张卡的卡包. 我们将 ...
- 【转】python中json.loads与eval的区别
JSON有两种结构: “名称/值”对的集合(A collection of name/value pairs).不同的语言中,它被理解为对象(object),纪录(record),结构(struct) ...
- thinkCMF----使用自定义函数
thinkCMF使用自定义函数:app 下新建 common.php
- 3种方法教你PS快速去掉水印
方法一:使用选框工具 勾选水印部分: 按住Shift+f5选择内容识别: 然后 ctrl+d 取消选择,水印就去掉了 PS:其实这个方法有个快捷办法,直接使用选框工具选中之后,按Delete就可以弹出 ...
- 解决ios safari中按钮圆角问题【原创】
问题描述 使用html5编写页面在移动app中嵌套,总会涉及到按钮的使用,在android手机浏览器中显示正常,但在ios safari浏览器中会看到按钮显示为圆角样式,设置border-rad ...