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, // 表示子 ...
随机推荐
- C++(四十)— C++中一个class类对象占用多少内字节
一个空的class在内存中多少字节?如果加入一个成员函数后是多大?这个成员函数存储在内存中什么部分? 一个Class对象需要占用多大的内存空间.最权威的结论是: 非静态成员变量总合. 加上编译器为了C ...
- WEB监控手段
WEB监控手段: 1.端口 本地: ss . netstat .lsof ss -ntlp|grep 80|wc -l (大于等于1) netsta ...
- Java精通并发-自旋对于synchronized关键字的底层意义与价值分析以及互斥锁属性详解与Monitor对象特性解说【纯理论】
自旋对于synchronized关键字的底层意义与价值分析: 对于synchronized关键字的底层意义和价值分析,下面用纯理论的方式来对它进行阐述,自旋这个概念就会应运而生,还是很重要的,下面阐述 ...
- linux系统编程之管道(二)
今天继续研究管道,话不多说,言归正传: 对于管道,有一定的读写规则,所以这里主要是对它的规则进行探讨,具体规则如下: 规则一: 下面用程序来验证下,还是用上节学的子进程写数据,父进程读取数据的例子,只 ...
- 「NOI2012」迷失游乐园
「NOI2012」迷失游乐园 题目描述 放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩. 进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐园抽象成有n个景点.m条道路的无向连通图,且该 ...
- Union-Find(并查集): Dynamic Connectivity 问题
设计算法一般所使用的方法过程 什么是Dynamic connectivity 我们的problem就是支持这两种操作: Union与connected query Example 问题是两个objec ...
- linux autofs自动挂载
autofs:自动挂载器 自动挂载器是一个监视目录的守护进程,并在目标子目录被引用时,自动执行预定义的NFS挂载 自动挂载器由autofs服务脚本管理 自动挂载器由auto.master配置文件进行配 ...
- javamail: UrlDataSource获取网络文件作为邮件的附件|javamail发送二进制流附件的问题
https://www.cnblogs.com/younldeace/p/5193103.html 最近做个邮件发送功能,要内嵌图片并有附件. 需求很奇怪,图片和附件文件是放在ftp服务器上的,查了下 ...
- thinkpadT470P安装问题
[问题描述]: 最近在将Thinkpad E430c的ubuntu系统重装成windows 7的过程中,出现了装好win7系统后,开机自动进入boot menu界面的问题,而且不论你选择从光驱还是硬盘 ...
- Mysql 基础 1
MySQL基础 1.概念 数据库,为我们提供高效.便捷的方式对数据进行增删改查的工具 优势 程序稳定.数据一致性.并发.效率 2.数据库管理系统 (DataBase Management System ...