【leetcode】827. Making A Large Island
题目如下:

解题思路:这个题目可以进行拆分成几个子问题。第一,求出island的数量,其实就是 200. Number of Islands,这个很简单,DFS或者BFS都能搞定;第二,除了求出island的数量之外,还要求出每个island包括的1的数量,这个也不难,在DFS或者BFS的过程中计数即可;第三,遍历grid中所有的0,判断每个0的上下左右分别连接了几个不同的island,并将连接的所有island的所有1的数量求和,再加上本身的1(0变来的)即是这个0变成1可以得到的large island,最后,求出所有0的最大的large island即可。
代码如下:
class Solution(object):
def largestIsland(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
for i in grid:
i.append('#')
i.insert(0,'#')
grid.append(['#' for i in grid[0]])
grid.insert(0,['#' for i in grid[0]])
visit = []
for i in grid:
visit.append([0 for x in i])
queue = [] l = [0] area = 1
num = 1 for i in xrange(1,len(grid)):
for j in xrange(1,len(grid[i])):
if grid[i][j] == '#' or grid[i][j] == 0:
continue
if visit[i][j] != 0:
continue
queue.append((i,j,area))
visit[i][j] = area
while len(queue) > 0:
x,y,a = queue.pop(0) # a为island的编号,用来记录一个有几个island
if grid[x+1][y] == 1 and visit[x+1][y] == 0:
num += 1
queue.append((x+1,y,a))
visit[x + 1][y] = a
if grid[x-1][y] == 1 and visit[x-1][y] == 0:
num += 1
queue.append((x-1,y,a))
visit[x - 1][y] = a
if grid[x][y+1] == 1 and visit[x][y+1] == 0:
num += 1
queue.append((x,y+1,a))
visit[x][y + 1] = a
if grid[x][y-1] == 1 and visit[x][y-1] == 0:
num += 1
queue.append((x,y-1,a))
visit[x][y - 1] = a
area += 1
l.append(num) #l为每个island的1的数量
num = 1
res = 0
for i in l:
if res < i:
res = i
#print visit,l
for i in xrange(1,len(grid)):
for j in xrange(1,len(grid[i])):
if grid[i][j] == 0:
count = 1
al = []
if grid[i+1][j] == 1:
if visit[i+1][j] not in al:
count += l[visit[i+1][j]]
al.append(visit[i+1][j])
if grid[i-1][j] == 1:
if visit[i-1][j] not in al:
count += l[visit[i-1][j]]
al.append(visit[i-1][j])
if grid[i][j+1] == 1:
if visit[i][j+1] not in al:
count += l[visit[i][j+1]]
al.append(visit[i][j+1])
if grid[i][j-1] == 1:
if visit[i][j-1] not in al:
count += l[visit[i][j-1]]
al.append(visit[i][j-1])
if res < count:
res = count
return res
【leetcode】827. Making A Large Island的更多相关文章
- 【LeetCode】695. Max Area of Island 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- 【LeetCode】830. Positions of Large Groups 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】817. Linked List Components 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- 那些年我们经历的BT面试题
初入职场面试的我到处碰壁,以下是我个人对几道面试题的小总结: 1.一列数字的规则如下:1,1,2,3,5,8,13,21, 34........ 求第30位数字是多少,用递规和非递归两种方法算法实现. ...
- CentOS6.5 编译安装PHP5.6(apache模块)
一.环境准备 1. 下载php源码包 # wget http://cn2.php.net/distributions/php-5.6.30.tar.gz # tar -xf php-5.6.30.ta ...
- Html5 Canvas斗地主游戏
过完年来公司,没什么事,主管说研究下html5 游戏,然后主管就给了一个斗地主的demo,随后我就开始看代码, 现在我看了html5以及canvas相关知识和斗地主的demo后,自己用demo上的素材 ...
- [转帖]mysql数据库主从配置
mysql数据库主从配置 https://www.toutiao.com/i6680489302947791371/ 多做实验 其实挺简单的 很多东西 要提高自信 去折腾. 架构与我 2019-04- ...
- nginx 事件机制原理
事件驱动模型是Nginx服务器保障完整功能和具有良好性能的重要机制之一. 事件驱动模型概述 实际上,事件驱动并不是计算机编程领域的专业词汇,它是一种比较古老的响应事件的模型,在计算机编程.公共关系.经 ...
- Linux hostname 主机名篇
主机名修改(以主机名为config为例) 1.修改文件/etc/sysconfig/network,内容为 [root@config ~]# cat /etc/sysconfig/network# C ...
- 循环Gray码的生成(非递归)
#!/usr/bin/env python #coding:utf-8 import sys def gray_code(n): if n < 1: return [] n += 1 array ...
- 关于微信H5页面开发中音乐不自动播放的解决方法
我想应该有很多人在做H5场景应用.H5微刊.H5微杂志的时候加入背景音乐吧(客户需求),相信很多人一定碰过不能自动播放的时候,即使是相同的iPhone 5s也有不播放的时候,很蛋疼吧!? 之前我的解决 ...
- RabbitMq学习3-工作队列(Work queues)
工作队列(又称:任务队列——Task Queues)是为了避免等待一些占用大量资源.时间的操作.当我们把任务(Task)当作消息发送到队列中,一个运行在后台的工作者(worker)进程就会取出任务然后 ...
- [LeetCode] 108. 将有序数组转换为二叉搜索树
题目链接 : https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/ 题目描述: 将一个按照升序排列的 ...