原题链接在这里: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:

 class Solution {
Map<Integer, Integer> row = new HashMap<>();
Map<Integer, Integer> colum = new HashMap<>();
int [] parent;
int [] size; public int removeStones(int[][] stones) {
int n = stones.length;
parent = new int[n];
size = new int[n];
for(int i = 0; i<n; i++){
parent[i] = i;
size[i] = 1;
} int count = n; for(int i = 0; i<n; i++){ int [] stone = stones[i]; if(!row.containsKey(stone[0])){
row.put(stone[0], i);
}else{
int p = row.get(stone[0]);
if(!find(i, p)){
union(i, p);
count--;
} } if(!colum.containsKey(stone[1])){
colum.put(stone[1], i);
}else{
int p = colum.get(stone[1]);
if(!find(i, p)){
union(i, p);
count--;
}
} } return n - count;
} private boolean find(int i, int j){
return root(i) == root(j);
} private int root(int i){
while(i != parent[i]){
parent[i] = parent[parent[i]];
i = parent[i];
} return i;
} private void union(int i, int j){
int p = root(i);
int q = root(j);
if(size[p] > size[q]){
parent[q] = p;
size[p] += size[q];
}else{
parent[p] = q;
size[q] += size[p];
}
}
}

LeetCode 947. Most Stones Removed with Same Row or Column的更多相关文章

  1. 【LeetCode】947. Most Stones Removed with Same Row or Column 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...

  2. 【leetcode】947. Most Stones Removed with Same Row or Column

    题目如下: On a 2D plane, we place stones at some integer coordinate points.  Each coordinate point may h ...

  3. Leetcode: 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 ...

  4. [Swift]LeetCode947. 移除最多的同行或同列石头 | 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 ...

  5. Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.

      1: /// <summary> 2: /// Write an algorithm such that if an element in an MxN matrix is 0, it ...

  6. excel小技巧-用于测试用例的编号栏:“获取当前单元格的上一格的值+1”=INDIRECT(ADDRESS(ROW()-1,COLUMN()))+1

    编写用例的时候使用,经常修改用例的时候会需要增加.删除.修改条目,如果用下拉更新数值的方式会很麻烦. 1.使用ctrl下拉,增删移动用例的时候,需要每次都去拉,万一列表比较长,会很麻烦 2.使用ROW ...

  7. Flutter 布局(七)- Row、Column详解

    本文主要介绍Flutter布局中的Row.Column控件,详细介绍了其布局行为以及使用场景,并对源码进行了分析. 1. Row A widget that displays its children ...

  8. params.row[params.column.key] vue h函数 当前单元格 h函数 div 属性 值或数组 render

    params.row[params.column.key] vue h函数 当前单元格 h函数 div 属性 值或数组 render

  9. Flutter 布局类组件:线性布局(Row和Column)

    前言 所谓线性布局,即指沿水平或垂直方向排布子组件.Flutter中通过Row和Column来实现线性布局,并且它们都继承自弹性布局(Flex). 接口描述 Row({ Key key, // 表示子 ...

随机推荐

  1. python入门基础 03

    整型 -- 数字 (int) 用于比较和运算的 32位 -2 ** 31 -1 ~ 2 ** 31 -1 64位 -2 ** 63 -1 ~ 2 ** 63 -1 + - * / // ** % &q ...

  2. 解决kali安装成功后没有声音的问题

    Kali Linux系统默认状态下,root用户是无法使用声卡的,也就没有声音.启用的方法如下: (1)在终端执行命令:systemctl --user enable pulseaudio (2)在/ ...

  3. Golang修改json文件的两种方法

    第三方包 go get -u github.com/tidwall/sjson bytes, _ := ioutil.ReadFile(jsonFile) value1, _ := sjson.Set ...

  4. MVC路由规则

    1 可以创建多条路由规则,每条路由规则的那么属性不同 2路由规则是有顺序的.如果被前面的规则匹配了,那么后面的规则就没机会了 3 constraints 约束: 4namespaces 命名空间 5r ...

  5. Gin上传文件到MongoDB gridfs

    目录 上传图片 下载图片 上传图片 func imagePost(c *gin.Context) { image, err := c.FormFile("file") if err ...

  6. Glances - Linux上的实时系统监控工具(Centos安装)

    Glances  WebServer 模式 在 glances 的 WebServer 模式下,客户端只通过浏览器访问就可以获取远程服务器的运行状态. 安装成功后,使用 glances -w 命令即可 ...

  7. Spring-Cloud之Sleuth链路追踪-8

    一.Spring Cloud Sleuth 是Spring Cloud 的一个组件,它的主要功能是在分布式系统中提供服务链路追踪的解决方案. 二.为什么需要Spring Cloud Sleuth? 微 ...

  8. Angular—入门环境,项目创建,导入项目

    1.安装 nodejs,npm nodejs 下载地址: https://nodejs.org/en/download/

  9. SQL Injection (Blind)

    Low级别基于布尔的盲注思路 1.判断是否存在注入,注入是字符型还是数字型 2.猜解当前数据库名 3.猜解数据库中的表名 4.猜解表中的字段名 5.猜解数据 判断是否有sql注入 输入1.1’ and ...

  10. MySQL数据库汇总

    -- mysql的最大连接数:默认为 100   -- mysql的增删改查   -- mysql统计各个字段(case when 用法 注:也可以使用其他的)   select (case when ...