zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    安利博客
    python 的高阶算法之堆排序
    functools模块用途
    类型注解
    高阶函数和装饰器
    生成器

    递归函数
    匿名函数
    函数 的 返回值作用域
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11229830.html
Copyright © 2011-2022 走看看