LeetCode 947. Most Stones Removed with Same Row or Column

原题链接在这里:https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/

题目:

On a 2D plane, we place stones at some integer coordinate points.  Each coordinate point may have at most one stone.

Now, a move consists of removing a stone that shares a column or row with another stone on the grid.

What is the largest possible number of moves we can make?

Example 1:

Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
Output: 5

Example 2:

Input: stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]
Output: 3

Example 3:

Input: stones = [[0,0]]
Output: 0

Note:

  1. 1 <= stones.length <= 1000
  2. 0 <= stones[i][j] < 10000

题解:

The number of most stones removed = stones count - unions count

If a stone in on a row or column same as other stone, then two stones are in the same union.

Use two HashMap to maintain row and column to stone index relationship. If the key already exist, then there is previous stone with same row or column existing already. 

Find parents of two stones, if they are not equal, then union.

Time Complexity: O(nlogn). find takes O(logn). Since use path compression and union by size, amortize O(1).

Space: O(n).

AC Java:

 1 class Solution {
 2     Map<Integer, Integer> row = new HashMap<>();
 3     Map<Integer, Integer> colum = new HashMap<>();
 4     int [] parent;
 5     int [] size;
 6     
 7     public int removeStones(int[][] stones) {
 8         int n = stones.length;
 9         parent = new int[n];
10         size = new int[n];
11         for(int i = 0; i<n; i++){
12             parent[i] = i;
13             size[i] = 1;
14         }
15         
16         int count = n;
17         
18         for(int i = 0; i<n; i++){
19 
20             int [] stone = stones[i];
21             
22             if(!row.containsKey(stone[0])){
23                 row.put(stone[0], i);
24             }else{
25                 int p = row.get(stone[0]);
26                 if(!find(i, p)){
27                     union(i, p);
28                     count--;
29                 }
30 
31             }
32             
33             if(!colum.containsKey(stone[1])){
34                 colum.put(stone[1], i);
35             }else{
36                 int p = colum.get(stone[1]);
37                 if(!find(i, p)){
38                     union(i, p);
39                     count--;
40                 }
41             }
42             
43         }
44         
45         return n - count;
46     }
47     
48     private boolean find(int i, int j){
49         return root(i) == root(j);
50     }
51     
52     private int root(int i){
53         while(i != parent[i]){
54             parent[i] = parent[parent[i]];
55             i = parent[i];
56         }
57         
58         return i;
59     }
60     
61     private void union(int i, int j){
62         int p = root(i);
63         int q = root(j);
64         if(size[p] > size[q]){
65             parent[q] = p;
66             size[p] += size[q];
67         }else{
68             parent[p] = q;
69             size[q] += size[p];
70         }
71     }
72 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11229830.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值