LeetCode - Min Remaining Chess Pieces
假设有一个棋盘(二维坐标系), 棋盘上摆放了一些石子(每个石子的坐标都为整数). 你可以remove一个石子, 当且仅当这个石子的同行或者同列还有其它石子. 输入是一个list of points. 问:
1) 给这些石子坐标, 你最多能remove多少个石子?
2) Follow-up: 若想保证remove的石子数量最大, 应按照什么顺序remove? (没有写代码)
DFS count connected components:
// "static void main" must be defined in a public class.
public class Main {
public static void main(String[] args) {
int[] a = {0,0};
int[] b = {0,1};
int[] c = {1,2};
int[] d = {2,3};
List<int[]> coords = new ArrayList<>();
coords.add(a);
coords.add(b);
coords.add(c);
coords.add(d); System.out.println(new Solution().minReminingChessPieces(coords));
}
}
class Solution{ public int minReminingChessPieces(List<int[]> coords){
if(coords == null){
return -1;
}
HashSet<String> visited = new HashSet<>();
int res = 0;
for(int[] coord : coords){
String s = coord[0]+":"+coord[1];
if(!visited.contains(s)){
res++;
DFSHelper(coords, coord, visited);
}
}
return res;
} public void DFSHelper(List<int[]> coords, int[] coord, HashSet<String> visited){
visited.add(coord[0]+":"+coord[1]);
for(int[] subCoord : coords){
if(subCoord[0] == coord[0] || subCoord[1] == coord[1]){
if(!visited.contains(subCoord[0]+":"+subCoord[1])){
DFSHelper(coords, subCoord, visited);
}
}
}
} }
LeetCode - Min Remaining Chess Pieces的更多相关文章
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- [LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- [LeetCode] Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode() Min Stack 不知道哪里不对,留待。
class MinStack { public: MinStack() { coll.resize(2); } void push(int x) { if(index == coll.size()-1 ...
- [leetcode] Min Stack @ Python
原题地址:https://oj.leetcode.com/problems/min-stack/ 解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列. 代码: class MinSta ...
- LeetCode: Min Stack 解题报告
Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...
- LeetCode——Min Stack
Description: Design a stack that supports push, pop, top, and retrieving the minimum element in cons ...
- Python3解leetcode Min Cost Climbing Stairs
问题描述: On a staircase, the i-th step has some non-negative cost cost[i]assigned (0 indexed). Once you ...
随机推荐
- 有时候union或者union all比左连接查询速度快
原来的语句 select count(1) from ( SELECT CustCode,ShopCode,CreateTime,UniqCode,SaleType,TotalMoney,Exami ...
- MySQL MERGE存储引擎
写这篇文章,主要是因为面试的时候,面试官问我怎样统计所有的分表(假设按天分表)数据,我说了两种方案,第一种是最笨的方法,就是循环查询所有表数据(肯定不能采用):第二种方法是,利用中间件,每天定时把前一 ...
- python常见问题汇总
1.python使用selenium中的时间等待 a.强制等待 time.sleep() b.隐式等待: 如果某些元素不是立即可用的,隐式等待是告诉WebDriver去等待一定的时间后去查找元素. 默 ...
- java项目 在 linux ubuntu 上的部署相关
--------------------JDK在linux ubuntu上的安装------------------------------------------------------------ ...
- 实例:用户界面控件Kendo UI vs DevExpress对比评测一
在一次使用ASP.NET MVC进行开发的Web项目中,我们需要用户界面控件来帮助实现界面的大部分功能.由于项目比较复杂,我们最终确定对 Kendo UI 和 DevExpress 这两款国际知名的用 ...
- 自动化运维之ansible
第三十九课 自动化运维之ansible 目录 十五. ansible介绍 十六. ansible安装 十七. ansible远程执行命令 十八. ansible拷贝文件或目录 十九. ansible远 ...
- 前端特效demo | 一起围观 10 种创意时钟
时钟动画应用非常广泛,我们经常会看到一些相当个性化的HTML5时钟动画.今天我们向大家分享 10 款形态各异的超时尚时钟动画,其中有圆盘时钟.创意时钟.电子时钟等,希望大家会喜欢. 文内附有时钟效果代 ...
- 解决nim db_mysql could not load: libmysql.dll的问题
title: 解决nim db_mysql could not load: libmysql.dll的问题 nim中使用db_mysql 操作数据库的代码看起来很简单: import db_mysql ...
- styled-components的基本使用
一.官网地址 https://www.styled-components.com/ 二.styled-components 1.styled-components 样式化组件,主要作用是它可以编写实际 ...
- 牛客练习赛 43 B-Tachibana Kanade Loves Probability
链接:https://ac.nowcoder.com/acm/contest/548/B 题目描述 立华奏在学习初中数学的时候遇到了这样一道大水题: “设箱子内有 n 个球,其中给 m 个球打上标记, ...