当前位置:网站首页>827. maximum man-made island and collection search
827. maximum man-made island and collection search
2022-06-21 06:19:00 【Empress Yu】
827. The largest artificial island
Give you a size of
n x nBinary matrixgrid. most Only one grid0become1.Return to... After performing this operation ,
gridWhat is the largest island area in the ?Islands By a group 、 Next 、 Left 、 Connected in four directions to the right
1formation .Example 1:
Input : grid = [[1, 0], [0, 1]] Output : 3 explain : One grid 0 become 1, Finally, connecting the two islands, we get an area of 3 The island of .Example 2:
Input : grid = [[1, 1], [1, 0]] Output : 4 explain : One grid 0 become 1, The area of the island expanded to 4.Example 3:
Input : grid = [[1, 1], [1, 1]] Output : 4 explain : No, 0 Can make us become 1, The area is still 4.Tips :
n == grid.lengthn == grid[i].length1 <= n <= 500grid[i][j]by0or1
The result of doing the question
success , The problem is very simple if you know how to search the set
step
1. Initialize parent node , Root node size
2. For adjacent 1 Connect
3. Yes 0 Location handling , Connect up, down, left and right 4 Maximum value after block , Special , Count yourself 1
details
1. whole 1 Handle , return n*n
2. whole 0 Handle , return 1( This step may not be handled )
3. Consider the case where the upper, lower, left and right blocks are adjacent , De duplication for parent node
class Solution {
int[] parent;
int[] sizes;
int n;
public int largestIsland(int[][] grid) {
n = grid.length;
parent = new int[n*n];
sizes = new int[n*n];
boolean allZero = true;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
int v = flat(i,j);
parent[v] = v;
sizes[v] = grid[i][j];
allZero = allZero && sizes[v]==0;
}
}
if(allZero) return 1;
int[] directions = new int[]{0,1,0,-1,0};
for(int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(grid[i][j]!=1) continue;
int t = flat(i,j);
for(int k = 0; k < 4; k++){
int x = i+directions[k];
int y = j+directions[k+1];
if(x>=0&&x<n&&y>=0&&y<n&&grid[x][y]==1){
connect(t,flat(x,y));
}
}
}
}
if(sizes[root(0)]==n*n) return n*n;
int ans = 0;
for(int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(grid[i][j]==1) continue;
int size = 1;
Set<Integer> p = new HashSet<>();
for(int k = 0; k < 4; k++) {
int x = i + directions[k];
int y = j + directions[k + 1];
if(x>=0&&x<n&&y>=0&&y<n && p.add(root(flat(x,y)))){
size += sizes[root(flat(x,y))];
}
}
ans = Math.max(size,ans);
}
}
return ans;
}
private int flat(int x, int y){
return x*n+y;
}
public void connect(int a, int b){
int rootA = root(a);
int rootB = root(b);
if(rootA == rootB) return;
if(sizes[rootA]>sizes[rootB]){
sizes[rootA]+=sizes[rootB];
parent[rootB] = rootA;
}else{
sizes[rootB]+=sizes[rootA];
parent[rootA] = rootB;
}
}
private int root(int v){
while (parent[v]!=v){
parent[v] = parent[parent[v]];
v = parent[v];
}
return v;
}
}边栏推荐
- Digital signal processing-07-dds IP application example
- Aurora8B10B IP使用 -01- 简介与端口描述
- tf. compat. v1.get_ default_ graph
- Copy the code generated by the code generator to the idea. How to solve the error reported by the web address after running
- Module 14 - 15: network application communication test
- 【数据挖掘】期末复习 第三章
- Leetcode 75 - three implementation methods of color classification [medium]
- [data mining] final review Chapter 2
- FPGA - 7系列 FPGA SelectIO -06- 逻辑资源之ODELAY
- Pyshark tutorial
猜你喜欢

The time plug-in is used for the establishment time, but when modifying parameters on the web page, if the time is not modified, an error will be reported when saving it for the first time, and it can

Aurora8b10b IP use-04-ip routine application example

FPGA - 7系列 FPGA SelectIO -06- 逻辑资源之ODELAY

Latest analysis on operation of refrigeration and air conditioning equipment in 2022 and examination question bank for operation of refrigeration and air conditioning equipment

Refine business details

Aurora8B10B IP使用 -01- 简介与端口描述

Idea usage record
![Leetcode 75 - 颜色分类 [Medium] 三种实现方法](/img/52/61ae051babf6b5c6b603093a17e55c.png)
Leetcode 75 - 颜色分类 [Medium] 三种实现方法

Solve the first problem of Huawei's machine test on April 20 by recursion and circulation (100 points)

Aurora8b10b IP usage-03-ip configuration application guide
随机推荐
[JVM] method area
牛客-TOP101-BM26
【数据挖掘】期末复习 第二章
docker 安装mysql
【数据挖掘】期末复习 第一章
构建和保护小型网络考试
nametuple的源码为什么要使用.replace(‘,‘, ‘ ‘).split()而不是.split(‘,‘)
After the code is generated by the code generator, the copy is completed, and the module is not displayed on the web page
当今的数学是否过于繁琐?
Is today's mathematics too cumbersome?
lambda-stream
IDEA 使用记录
Introduction to 3D engine software wizard
模块 14 - 15:网络应用通信考试
FPGA - 7 Series FPGA selectio -03- ILOGIC of logic resources
Roll in Dachang series LRU cache elimination algorithm
Detailed explanation of balanced binary tree is easy to understand
FPGA - 7系列 FPGA SelectIO -05- 逻辑资源之OLOGIC
Module 14 - 15: network application communication test
Broadcast mechanism of numpy