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 ...
随机推荐
- 使用Selenium IDE和webDriver进行自动化软件测试
1.Selenium IDE 在Chrome浏览器上登录谷歌应用商店可以安装Selenium IDE插件(3.0以上版本的Selenium IDE不支持录制的脚本导出,所以这里使用到的是应用商店上的另 ...
- spring cloud微服务下手动回滚事务
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 这里使用的场景是,跨服务调用接口,比如:用户信息和用户积分 ...
- 试试Markdown哈
目录 一级标题 二级标题 三级标题 二级标题? 我擦了? 这什么语法.文字下面加-号,实现二级标题? 看看是几级标题 还真的是二级标题. ...... # 看来四个空格是个,嗯,默认的东西 ??中间是 ...
- Oracle查询和过滤重复数据
对数据库某些意外情况,引起的重复数据,如何处理呢? ----------------查重复: select * from satisfaction_survey s and s.project_no ...
- 【Shell】单行注释和多行注释
单行注释 '# ' # echo "hello" 多行注释 方法1 : << ! 这是注释1 这是注释2 这是注释3 ! 方法2 :' 这是注释1 这是注释2 这是注释 ...
- arcgis api for js 4.4 绘图小工具
目前在4.x API中还未有官方的绘图工具,而实际项目中又需要这样的绘图工具,所以自己写了一个. 奉上代码. 使用方法:1.将引入的API模块传入构造函数 var drawTools = new D ...
- web应用及web.xml
一.创建web应用 1.在任意目录新建webDemo文件夹 2.在webDemo下新建WEB-INF文件夹(注意大小写) 3.在WEB-INF中新建web.xml文件(可以copy已有的web应用中的 ...
- clusterware启动顺序——OHASD
Clusterware启动顺序 [root@ebsdb1 etc]# crsctl check crs CRS-4638: Oracle High Availability Services is ...
- Groovy学习笔记-Java 5新特性支持
1.枚举enum enum CoffeeSize{ SHORT, SMALL, MEDIUM, LARGE, MUG } def orderCoffee(size){ print "Coff ...
- 如何使用HTML5的WebSocket实现网页与服务器的双工通信(二)
本系列服务端双工通信包括两种实现方式:一.使用Socket构建:二.使用WCF构建.本文为使用WCF构建服务端的双工通信,客户端同样使用Html5的WebSocket技术进行调用. 一.创建WCF服务 ...