当前位置:网站首页>Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
2022-07-05 18:04:00 【REN_ Linsen】
dfs And connected components
Preface
How to find the total number of all nodes of connected components ?dfs+visited
How to record the total number of nodes of the connected component for all nodes of the connected component ? Change the variable to an array ( We use a heap address )
How to determine whether the nodes around a node are two different connected components ? Number the connected components or Union checking set findFather
One 、 The largest artificial island
Two 、DFS+ Search variant
package everyday.hard;
import java.util.HashSet;
import java.util.Set;
// The largest artificial island .
public class LargestIsland {
/* target: Only one grid can be removed from 0 Turn into 1, Look at the largest island area . intuitive :DFS Find island area , How to draw inferences from one instance ? Since only one 0 Turn into 1, Then from 0 The place of dfs, But the time complexity is very high !O(N * N * N * N) Then trade space for time , Pass each block in advance dfs Well done , From 1 The place of dfs, Each location records the total number of connected blocks . however , This will fail , because 0 The left and right connected blocks of may not be independent connected blocks , It may be the same connected block , It will lead to double calculation . therefore , You need to number connected component blocks , Different connected component blocks can be added together . */
public int largestIsland(int[][] grid) {
int m = grid.length, n = grid[0].length;
int max = 1 << 31;
int[][][] f = new int[m][n][2];
// from 1 set out dfs
int idx = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
int[] total = new int[]{
0, idx};
dfs(grid, i, j, f, total);
max = Math.max(max, total[0]);
++idx;
}
}
}
// from 0 set out dfs.
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 0) {
int total = 1;
// To duplicate the connected components
Set<Integer> bool = new HashSet<>();
for (int[] gap : gaps) {
int ni = i + gap[0], nj = j + gap[1];
if (ni == -1 || -1 == nj || nj == grid[0].length || grid.length == ni || grid[ni][nj] == 0)
continue;
if (!bool.contains(f[i][j][1])) {
total += f[i][j][0];
bool.add(f[i][j][1]);
}
}
max = Math.max(max, total);
}
}
}
return max;
}
// It is convenient to walk in four directions , Use matrix + The way of circulation .
static final int[][] gaps = new int[][]{
{
1, 0}, {
-1, 0}, {
0, 1}, {
0, -1}};
// How to make all nodes of a connected component be assigned to the total number of nodes .
private void dfs(int[][] grid, int i, int j, int[][][] f, int[] total) {
if (i == -1 || -1 == j || j == grid[0].length || grid.length == i || grid[i][j] <= 0) return;
total[0]++;
f[i][j] = total;
grid[i][j] = -1;
for (int[] gap : gaps) {
int ni = i + gap[0], nj = j + gap[1];
dfs(grid, ni, nj, f, total);
}
}
}
summary
1) The question is DFS+ Improved version of memory search , What you want to do is draw inferences from one instance , We must grasp the details firmly , For example, the visited needs are always marked , You can't leave it unmarked , You can't go back and remove the mark , This is not to find the maximum path value ; If the subscript is judged to be out of bounds , No i == 0 || i == n, yes i == -1 || i == n, This can be mistaken , I'm impressed .
2) Master these basic knowledge , According to the needs of the topic , To make corresponding use . For example, this topic can be a 0 become 1, Then it's better to start from 0 We're going to start traversing , Taking the maximum .
3) It's rather sad to do this today , Always feel that the idea is right ( But the idea is right , It's just that there's a hole that hasn't been found .), It took a lot of time to find the key problem .( The ability to find key problems is too weak , I think this is the core ability !). But don't be discouraged , Read the explanation of the question , Only to find that this is a hole deliberately given by the official , Need to break the comfort zone knowledge , Learn to draw inferences from one example , Grow step by step . So laugh if you want to lose , Read your thoughts out loud , If you can't figure it out, learn to solve the problem , This is a good time to break through yourself .
reference
边栏推荐
- Sophon KG升级3.1:打破数据间壁垒,解放企业生产力
- 隐私计算助力数据的安全流通与共享
- 破解湖+仓混合架构顽疾,星环科技推出自主可控云原生湖仓一体平台
- Configure pytorch environment in Anaconda - win10 system (small white packet meeting)
- Why is all (()) true and any (()) false?
- ISPRS2022/雲檢測:Cloud detection with boundary nets基於邊界網的雲檢測
- Size_ T is unsigned
- 【PaddleClas】常用命令
- Sophon CE Community Edition is online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool
- 记一次使用Windbg分析内存“泄漏”的案例
猜你喜欢
Tencent music launched its new product "quyimai", which provides music commercial copyright authorization
论文阅读_医疗NLP模型_ EMBERT
南京大学:新时代数字化人才培养方案探讨
How awesome is the architecture of "12306"?
小林coding的内存管理章节
Ten capabilities that cyber threat analysts should have
ISPRS2020/云检测:Transferring deep learning models for cloud detection between Landsat-8 and Proba-V
ISPRS2022/雲檢測:Cloud detection with boundary nets基於邊界網的雲檢測
Star Ring Technology launched transwarp Navier, a data element circulation platform, to help enterprises achieve secure data circulation and collaboration under privacy protection
Anaconda中配置PyTorch环境——win10系统(小白包会)
随机推荐
神经网络自我认知模型
Gimp 2.10 tutorial "suggestions collection"
Redis Foundation
第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”
Cmake tutorial Step2 (add Library)
Data access - entityframework integration
Sophon AutoCV:助力AI工业化生产,实现视觉智能感知
Elk log analysis system
开户复杂吗?网上开户安全么?
从类生成XML架构
修复漏洞 - mysql 、es
Six bad safety habits in the development of enterprise digitalization, each of which is very dangerous!
深拷贝与浅拷贝【面试题3】
U-Net: Convolutional Networks for Biomedical Images Segmentation
Which platform of outer disk gold is regular and safe, and how to distinguish it?
[performance test] full link voltage test
How can cluster deployment solve the needs of massive video access and large concurrency?
leetcode每日一练:旋转数组
tkinter窗口预加载
MATLAB查阅