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 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 <= stones.length <= 1000
0 <= stones[i][j] < 10000
If stone a
and stone b
are in the same column/row, we connect them as a component
The largest possible number of moves we can make = the number of unions we can make = Total stones count - count of components.
class Solution {
public int removeStones(int[][] stones) {
UnionFind uf = new UnionFind(stones.length);
int countUnion = 0;
for (int i = 0; i < stones.length - 1; i ++) {
for (int j = i + 1; j < stones.length; j ++) {
if (uf.isConnected(i, j)) continue;
if (stones[i][0] == stones[j][0] || stones[i][1] == stones[j][1]) {
uf.union(i, j);
countUnion ++;
}
}
}
return countUnion;
} public class UnionFind {
int[] fathers; public UnionFind(int n) {
fathers = new int[n];
for (int i = 0; i < n; i ++) {
fathers[i] = i;
}
} public void union(int i, int j) {
int iRoot = find(i);
int jRoot = find(j);
fathers[jRoot] = iRoot;
} public int find(int i) {
while (i != fathers[i]) {
i = fathers[i];
}
return i;
} public boolean isConnected(int i, int j) {
return find(i) == find(j);
}
}
}
Leetcode: Most Stones Removed with Same Row or Column的更多相关文章
- 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 ...
- 【LeetCode】947. Most Stones Removed with Same Row or Column 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...
- 【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 ...
- [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 ...
- 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 ...
- excel小技巧-用于测试用例的编号栏:“获取当前单元格的上一格的值+1”=INDIRECT(ADDRESS(ROW()-1,COLUMN()))+1
编写用例的时候使用,经常修改用例的时候会需要增加.删除.修改条目,如果用下拉更新数值的方式会很麻烦. 1.使用ctrl下拉,增删移动用例的时候,需要每次都去拉,万一列表比较长,会很麻烦 2.使用ROW ...
- Flutter 布局(七)- Row、Column详解
本文主要介绍Flutter布局中的Row.Column控件,详细介绍了其布局行为以及使用场景,并对源码进行了分析. 1. Row A widget that displays its children ...
- params.row[params.column.key] vue h函数 当前单元格 h函数 div 属性 值或数组 render
params.row[params.column.key] vue h函数 当前单元格 h函数 div 属性 值或数组 render
- Flutter 布局类组件:线性布局(Row和Column)
前言 所谓线性布局,即指沿水平或垂直方向排布子组件.Flutter中通过Row和Column来实现线性布局,并且它们都继承自弹性布局(Flex). 接口描述 Row({ Key key, // 表示子 ...
随机推荐
- SecurityProtocolType 枚举
地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.net.securityprotocoltype?redirectedfrom=MSDN&a ...
- java 图片base64互转
public class ImgBase64 { public static void main(String[] args) //测试 { String strImg = GetImageStr() ...
- mysqldump简单备份
mysqldump简单命令 mysqldump DB_NAME [tb1] [tb2] > /path/back.sql --master-data={0|1|2} 0: 不记录二进制日志 ...
- python高级特性-生成器
在python中一边循环一边计算的机制成为生成器(generator) 在每次调用next()的时候执行,遇到yield语句返回,再次执行时从上次返回的yield语句处继续执行. 生成list > ...
- No.1.测试Synchronized加锁String字符串后的多线程同步状况
测试目的描述 Synchronized关键字锁定String字符串可能会带来严重的后果, 尽量不要使用 synchronized(String a) 因为JVM中,因为字符串常量池具有缓冲功能! 接下 ...
- 《你说对就队》第九次团队作业:【Beta】Scrum meeting 2
<你说对就队>第九次团队作业:[Beta]Scrum meeting 2 项目 内容 这个作业属于哪个课程 [教师博客主页链接] 这个作业的要求在哪里 [作业链接地址] 团队名称 < ...
- 结构型模式(一) 适配器模式(Adapter)
一.动机(Motivation) 在软件系统中,由于应用环境的变化,常常需要将"一些现存的对象"放在新的环境中应用,但是新环境要求的接口是这些现存对象所不满足的. 如何应对这种&q ...
- 在linux系统中安装MySQL
1.安装Linux系统中自带的MySQL安装包 在现在常用的发行版本里都集中了MySQL安装包 CentOS系统中的YUM中包含了MySQL安装包,版本是MySQL5,rpm软件包的名称是mysql- ...
- springboot+jpa多表查询
背景 关联起来查询三个表的数据 步骤 定义接收结果的中间类 在 @Query 中编写 HSQL 从数据库查询 代码 定义接收结果的中间类 IterationVo.java /** * Iteratio ...
- Java四种读取和创建XML文档的例子教程
四种方法解析XML文档:Dom.SAX.JDOM.dom4j 1.了解XML XML,即可扩展标记语言(Extensible Markup Language),标准通用标记语言的子集 ...