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的更多相关文章

  1. 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 ...

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

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

  3. 【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 ...

  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算法与数据结构-希尔排序算法(35)

    一.希尔排序的介绍 希尔排序(Shell Sort)是插入排序的一种.也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本.希尔排序是非稳定排序算法. 希尔排序是把记录按下标的一定增量分组,对每 ...

  2. springboot搭建环境之使用@Slf4j注解方式,进行日志管理

    如果不想每次都写private  final Logger logger = LoggerFactory.getLogger(XXX.class); 可以用注解@Slf4j 需要引入依赖为: < ...

  3. 虚拟机安装Linux从零到登陆成功教学

    1.Linux Linux使我们出来windows以外可能接触最多的操作系统了,因为好多超级大的互联网公司,比如阿里等就是用Linux的,所以我们最起码要知道怎样去使用,使用的前提就是我们要有一个这样 ...

  4. test20190815 NOIP2019 模拟题

    100+60+40=200,被后面两个题卡着我很不爽. 立方数 [问题描述] 作为 XX 战队的狂热粉丝,MdZzZZ 看到了自己心仪的队伍在半决赛落败,顿时 心灰意冷.看着自己手中的从黄牛那里抢来的 ...

  5. SVM: 使用kernels(核函数)的整个SVM算法过程

    将所有的样本都选做landmarks 一种方法是将所有的training data都做为landmarks,这样就会有m个landmarks(m个trainnign data),这样features就 ...

  6. 4.1 vue-resource

    全局拦截器.配置全局地址等:

  7. Python文件的读写操作

    Python文件的使用 要点:Python能够以文本和二进制两种形式处理文件. 1.文件的打开模式,如表1:  注意:使用open()函数打开文件,文件使用结束后耀使用close()方法关闭,释放文件 ...

  8. RookeyFrame 模块 线上创建的模块 迁移到 线下来

    1. 把线上创建的model,写在项目的model层里面. 把文件 Rookey.Frame.Web\Config\TempModel\Order_File.code 复制到model层, 用文本编辑 ...

  9. RookeyFrame 加载 自定义JS

    注意JS存放的位置:是在model文件夹下的某某文件夹!!! 线上添加的模块: 1.JS文件名:和表名一致 2.JS目录:Rookey.BusSys.Web\Scripts\model\TempMod ...

  10. zabbix 内置key说明

    原文参考:https://blog.csdn.net/whs_321/article/details/52939263 一.简介 Zabbix 内置了很多丰富的key,使得我们在添加linux os模 ...