假设有一个棋盘(二维坐标系), 棋盘上摆放了一些石子(每个石子的坐标都为整数). 你可以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的更多相关文章

  1. leetCode Min Stack解决共享

    原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...

  2. [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 ...

  3. [LeetCode] Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  4. [LeetCode] Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  5. LeetCode() Min Stack 不知道哪里不对,留待。

    class MinStack { public: MinStack() { coll.resize(2); } void push(int x) { if(index == coll.size()-1 ...

  6. [leetcode] Min Stack @ Python

    原题地址:https://oj.leetcode.com/problems/min-stack/ 解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列. 代码: class MinSta ...

  7. LeetCode: Min Stack 解题报告

    Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...

  8. LeetCode——Min Stack

    Description: Design a stack that supports push, pop, top, and retrieving the minimum element in cons ...

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

随机推荐

  1. 解决Warning: mysql_connect(): Headers and client library minor version mismatch. 警告

    php -i|grep Client 查询当前Client 版本,结果如下: Client API version => 5.6.31Client API library version =&g ...

  2. 4 扩展库Scipy

    https://www.scipy.org/ 1. numpy    矩阵 2. matplotlib 绘图库 3. pandas 高效的Series和DataFrame数据结构 4.5 ndarry ...

  3. C# 索引和长度必须引用该字符串内的位置 LENGTH

    今天遇到了 索引和长度必须引用该字符串内的位置 的问题. 原因是实用 Substring 对字符串进行了前五位的截取,但是忽略了字符串本身不足五位的情况. 如果不足五位,直接将整个字符串赋值过来,添加 ...

  4. admin 自定义字段颜色 并加以简单判断

    在model中class Books(models.Model): nid = models.AutoField(primary_key=True, ) title = models.CharFiel ...

  5. RobotFramework Selenium2 关键字

    *** Settings ***Library Selenium2Library *** Keywords ***Checkbox应该不被选择 [Arguments] ${locator} Check ...

  6. python3 进行字符串、日期、时间、时间戳相关转换

    1.字符串转换成时间戳 2. 日期转换成时间戳

  7. 来自C++之父的建议

    You don’t have to know every detail of C++ to write good programs; Focus on programming techniques, ...

  8. .net下的缓存技术

    1.为什么要缓存?缓存能解决的问题 1.1稳定性 同一个应用中,对同一数据.逻辑功能和用户界面的多次请求时经常发生的.当用户基数很大时,如果每次请求都进行处理,消耗的资源是很大的浪费,也同时造成系统的 ...

  9. Open Daylight integration with OpenStack: a tutorial

    Open Daylight integration with OpenStack: a tutorial How to deploy OpenDaylight and integrate it wit ...

  10. Android第二次作业

    另一成员链接:https://www.cnblogs.com/2575590018dqy/p/10053353.html