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 ...
随机推荐
- mysql事件调用存储过程总结
第一次写事件调用存储过程,在网上找了一些资料,特此做下总结,巩固一下: 事件调用存储过程主要有三种: (1)创建事件马上执行,调用存储过程 CREATE EVENT if not exists Eve ...
- tomcat 内存溢出处理方案
找到tomcat7w.exe 在java 页 java options 最后添加 -XX:PermSize=256m-XX:MaxPermSize=512m
- 20165214 2018-2019-2 《网络对抗技术》Exp3 免杀原理与实践 Week5
<网络对抗技术>Exp3 免杀原理与实践 Week5 一.实验内容 1.正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,加壳工具,使用shell ...
- java HttpClient 忽略证书的信任的实现 MySSLProtocolSocketFactory
当不需要任何证书访问https时,java中先实现一个MySSLProtocolSocketFactory类忽略证书的信任 package com.tgb.mq.producer.utils; imp ...
- esp8266网络自动对时 串口字符连接 并显示 12864i2c u8g2库
给别人定做的 做的 集成了 烟雾传感器dht11u8g2网络 自动对时 #include <dht11.h>//程序中调用了dht11的库#include <Arduino.h> ...
- JAVA8之数据流Stream
在JAVA8之前的传统编程方式,如果我们需要操作一个集合数据,需要使用集合提供的API,通过一个循环去获取集合的元素,这种访问数据的方式会使代码显得臃肿,JAVA8新引入的Stream类,用于重新封装 ...
- 手动调用run方法和普通方法调用没有区别
手动调用run方法和普通方法调用没有区别
- WebStorm 安装及使用
WebStrom 插件安装 File(文件) -> settings(设置) -> Plugins 即可调出设置中的插件选项. 或者 按 快捷键ctrl + alt + s也可调出设置菜单 ...
- C语言---指针变量详解1
数据在内存中的地址也称为指针,如果一个变量存储了一份数据的指针,我们就称它为指针变量.在C语言中,允许用一个变量来存放指针,这种变量称为指针变量.指针变量的值就是某份数据的地址,这样的一份数据可以是数 ...
- 模糊查询出list
<resultMap id="shipList" type="com.sinochem.yunlian.yunliantraffic.domain.Ship&quo ...