惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
D
Docker
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
Jina AI
Jina AI
酷 壳 – CoolShell
酷 壳 – CoolShell
M
MIT News - Artificial intelligence
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
B
Blog RSS Feed
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale
Vercel News
Vercel News
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
有赞技术团队
有赞技术团队
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
I
InfoQ
U
Unit 42

Long Luo's Life Notes

夏至日测地球:利用太阳影子计算地球半径 2009年江西高考数学压轴题:陶平生老师又藏了什么数学机关? 《茶杯里的风暴》读书笔记:从日常生活中的小事,看懂背后的物理学 2008年江西高考数学压轴题详解:为什么它被称为史上最难高考数学题? 太阳温度是怎么计算出来的? 《大象的时间,老鼠的时间》读书笔记:生命节奏背后的数学规律 小港流到哪里去? 如何用一根棍子测出地球有多大?复刻埃拉托色尼的春分实验 2007江苏高考数学第20题解析:一道通向黄金分割数的数列压轴题 Google经典面试题: 鸡蛋应该怎么扔? 2010年江苏高考数学压轴题解析:巧用余弦定理与数学归纳法 2011年清华大学自主招生数学题解析:一道经典数列题的解法与思路 2011年清华大学自主招生数学题解析:一道经典数列题的解法与思路 2006年江西高考理科数学压轴题解析:递推、放缩与不等式结构 2006年江西高考理科数学压轴题解析:递推、放缩与不等式结构 一道初中数学极值题的多种解法:柯西不等式、几何法、函数法详解 扔几个骰子,怎么算出期望?——拼多多校招笔试算法题的数学故事 拼多多校招笔试算法题:一行公式搞定“多多的魔术盒子” 斯特林公式(Stirling's Formula):我一个阶乘表达式,怎么就和圆扯上关系了呢? 我爱做题:2010年江西高考理科数学压轴题 热机的效率上限在哪里?解析卡诺循环(Carnot Cycle) 为什么 2024 年会有 366 天? 数学之美:几何视角下的高斯积分(Gaussian Integral) 从最小二乘法到正态分布:高斯是如何找到失踪的谷神星的? 正态分布(Normal Distribution)公式为什么长这样? 高速公路编号背后的数学密码 2024阿里巴巴全球数学竞赛预选赛试题及解答 库函数 (libm) 是如何计算三角函数值的? payne hanek 归约算法 音乐背后的数学
LeetCode 947. Most Stones Removed with Same Row or Column...
2022-11-14 · via Long Luo's Life Notes

By Long Luo

This article is the solution It is Literally a Graph: DFS and Union Find of Problem 947. Most Stones Removed with Same Row or Column .

Intuition

We can find that this is a graph theory problem with analysis.

Imagine the stone on the 2D coordinate plane as the vertex of the graph, If the x-coord or the y-coord of two stones are the same, there is an edge between them.

This can be show as follows:

947. Most Stones Removed with Same Row or Column 1

According to the rule that stones can be removed, we should remove those points that are in the same row or column with other points as late as possible. That is, the more points in the same row or column with point A, the later point A should be removed. In this way, we can delete as many points as possible through point A.

It can be found that all vertices in a connected graph can be deleted to only one vertex.

947. Most Stones Removed with Same Row or Column 2

Since these vertices are in a connected graph, all vertices of the connected graph can be traversed by DFS or BFS.

Therefore: the maximum number of stones that can be removed = the number of all stones - the number of connected components.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public int removeStones(int[][] stones) {
int n = stones.length;
if (n <= 1) {
return 0;
}

List<Integer>[] graph = new List[n];
for (int i = 0; i < n; i++) {
graph[i] = new ArrayList<>();
}

for (int i = 0; i < n; i++) {
int[] u = stones[i];
for (int j = 0; j < n; j++) {
if (i == j) {
continue;
}

int[] v = stones[j];
if (u[0] == v[0] || u[1] == v[1]) {
graph[i].add(j);
}
}
}

boolean[] visited = new boolean[n];
int ans = 0;

for (int i = 0; i < n; i++) {
if (visited[i]) {
continue;
}

dfs(graph, visited, i);
ans++;
}

return n - ans;
}

private static void dfs(List<Integer>[] graph, boolean[] visited, int start) {

visited[start] = true;

List<Integer> neighbors = graph[start];
for (int x : neighbors) {
if (visited[x]) {
continue;
}

dfs(graph, visited, x);
}
}
}

Analysis

  • Time Complexity: \(O(n^2)\).
  • Space Complexity: \(O(n)\).

Union Find

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
public int removeStones(int[][] stones) {
if (stones == null || stones.length <= 1) {
return 0;
}

int n = stones.length;

UnionFind uf = new UnionFind();
for (int[] edge : stones) {
uf.union(edge[0] + 10001, edge[1]);
}

return n - uf.getCount();
}

class UnionFind {
Map<Integer, Integer> parents;
int count;

public UnionFind() {
parents = new HashMap<>();
count = 0;
}

public int getCount() {
return count;
}

public int find(int x) {
if (!parents.containsKey(x)) {
parents.put(x, x);
count++;
}

if (x != parents.get(x)) {
parents.put(x, find(parents.get(x)));
}

return parents.get(x);
}

public void union(int x, int y) {
int rootX = find(x);
int rootY = find(y);
if (rootX == rootY) {
return;
}

parents.put(rootX, rootY);
count--;
}
}
}

Analysis

  • Time Complexity: \(O(n \log n)\)
  • Space Complexity: \(O(n)\)

All suggestions are welcome. If you have any query or suggestion please comment below. Please upvote👍 if you like💗 it. Thank you:-)

Explore More Leetcode Solutions. 😉😃💗