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 island? (An island is a 4-directionally connected group of 1s).

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的更多相关文章

  1. [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 ...

  2. 【leetcode】827. Making A Large Island

    题目如下: 解题思路:这个题目可以进行拆分成几个子问题.第一,求出island的数量,其实就是 200. Number of Islands,这个很简单,DFS或者BFS都能搞定:第二,除了求出isl ...

  3. 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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. 【leetcode】Max Area of Island

    国庆中秋长假过完,又要开始上班啦.先刷个题目找找工作状态. Given a non-empty 2D array grid of 0's and 1's, an island is a group o ...

  8. Java实现 LeetCode 827 最大人工岛(DFS+暴力模拟)

    827. 最大人工岛 在二维地图上, 0代表海洋, 1代表陆地,我们最多只能将一格 0 海洋变成 1变成陆地. 进行填海之后,地图上最大的岛屿面积是多少?(上.下.左.右四个方向相连的 1 可形成岛屿 ...

  9. [Leetcode]827.使用回溯+标记解决最大人工岛问题

    在二维地图上, 0代表海洋, 1代表陆地,我们最多只能将一格 0 海洋变成 1变成陆地. 进行填海之后,地图上最大的岛屿面积是多少?(上.下.左.右四个方向相连的 1 可形成岛屿) 示例 1: 输入: ...

随机推荐

  1. python爬虫---->scrapy的使用(一)

    这里我们介绍一下python的分布式爬虫框架scrapy的安装以及使用.平庸这东西犹如白衬衣上的污痕,一旦染上便永远洗不掉,无可挽回. scrapy的安装使用 我的电脑环境是win10,64位的.py ...

  2. LeetCode——Nim Game

    Description: You are playing the following Nim Game with your friend: There is a heap of stones on t ...

  3. C# 反射的深入了解

    Assembly.Load("")的使用说明如下;     并不是命名空间.常用的是程序集名称,也就是dll的名称 关于反射Assembly.Load("程序集" ...

  4. 使用java实现的socket代理(支持socket4和socket5)

    代码如下: import java.io.*; import java.net.InetAddress; import java.net.ServerSocket; import java.net.S ...

  5. 深入浅出WPF之Binding的使用(二)

    在上一篇中介绍了Binding的基本绑定方法,这一篇中我们在深入的介绍Binding的其他用法. Binding的源也就是数据的源头,在日常的工作中,除了使用像上一篇中的Student对象作为数据源外 ...

  6. ALTERA FPGA Quartus 指定memory综合使用 M4K块

    最近遇到个问题, 使用二位数组方式定义了一个RAM ,但是软件每次 都是使用逻辑单元综合 这块memory  , 在ALTERA的网页上 找到了 方法,,在定义的 memory前面加一句画 (* ra ...

  7. Spring JPA配置讲解

    JPA是Java EE5规范之一,是一个orm规范,由厂商来实现该规范.目前有hibernate,OpenJPA,TopLink和EclipseJPA等实现 Spring提供三种方法集成JPA: 1. ...

  8. Solve minGW g++ has stopped working 程序停止运行

    之前在机子装了个很早版本的MinGW,苦于不支持c++11,所以打算卸载掉安装个新版本的.可是网上找了很多版本装好后,编译成功,运行的时候总是弹出 *.exe has stopped working的 ...

  9. linux使用bin文件安装jdk

    jdk1.6.20文件为bin文件安装过程如下 添加执行权限 chmod +x jdk-6u20-linux-x64.bin 运行,出现提示需要输入yes ./jdk-6u20-linux-x64.b ...

  10. NBUTOJ 1643 - 阶乘除法 - [数学题]

    题目链接:https://ac.2333.moe/Problem/view.xhtml?id=1643 问题描述 输入两个正整数 n, m,输出 n!/m!,其中阶乘定义为 n!= 1*2*3*... ...