947. Most Stones Removed with Same Row or Column Medium

@problem@discussion
#Depth-First Search#Union Find#Graph



1/**
2 * [947] Most Stones Removed with Same Row or Column
3 *
4 * On a 2D plane, we place n stones at some integer coordinate points. Each coordinate point may have at most one stone.
5 * A stone can be removed if it shares either the same row or the same column as another stone that has not been removed.
6 * Given an array stones of length n where stones[i] = [xi, yi] represents the location of the i^th stone, return the largest possible number of stones that can be removed.
7 *  
8 * Example 1:
9 * 
10 * Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
11 * Output: 5
12 * Explanation: One way to remove 5 stones is as follows:
13 * 1. Remove stone [2,2] because it shares the same row as [2,1].
14 * 2. Remove stone [2,1] because it shares the same column as [0,1].
15 * 3. Remove stone [1,2] because it shares the same row as [1,0].
16 * 4. Remove stone [1,0] because it shares the same column as [0,0].
17 * 5. Remove stone [0,1] because it shares the same row as [0,0].
18 * Stone [0,0] cannot be removed since it does not share a row/column with another stone still on the plane.
19 * 
20 * Example 2:
21 * 
22 * Input: stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]
23 * Output: 3
24 * Explanation: One way to make 3 moves is as follows:
25 * 1. Remove stone [2,2] because it shares the same row as [2,0].
26 * 2. Remove stone [2,0] because it shares the same column as [0,0].
27 * 3. Remove stone [0,2] because it shares the same row as [0,0].
28 * Stones [0,0] and [1,1] cannot be removed since they do not share a row/column with another stone still on the plane.
29 * 
30 * Example 3:
31 * 
32 * Input: stones = [[0,0]]
33 * Output: 0
34 * Explanation: [0,0] is the only stone on the plane, so you cannot remove it.
35 * 
36 *  
37 * Constraints:
38 * 
39 * 	1 <= stones.length <= 1000
40 * 	0 <= xi, yi <= 10^4
41 * 	No two stones are at the same coordinate point.
42 * 
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/
47// discuss: https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52    pub fn remove_stones(stones: Vec<Vec<i32>>) -> i32 {
53        0
54    }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_947() {
65    }
66}
67


Back
© 2025 bowen.ge All Rights Reserved.