假设有一个棋盘(二维坐标系), 棋盘上摆放了一些石子(每个石子的坐标都为整数). 你可以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. python识别图片中的代码。

    在看并发编程网的时候,有些示例代码是以图片的形式出现的,要是此时自己想复制下来的话,只能对着图片敲了,很不爽,于是搜了一下识别图片的网站,有! 把图片上传上去解析,下来txt文本,打开一看,大部分能解 ...

  2. python-之-深浅拷贝二(元组)

    元组比较特殊 1.----元组本身为不可变类型 import copy v1 = (1, 2, 3, 4) v2 = copy.copy(v1) print(id(v1), id(v2)) v3 = ...

  3. QSSP软件一些参数的设置(远震波形合成)

    1.time window, sampling interval 这里需要注意的是两者的和必须是2的n次方,可以写成2047 1; 2046 2,或2047.75 0.25,2047.9 0.1等等 ...

  4. H5页面访问java后台进行登录拦截

    1.未登录状态下进行拦截,回到登录页面 function judgeLogin(currentPage) { var judge=false; var storage=window.localStor ...

  5. Object.assign的用法

    工作中使用的Object.assign 类的赋值 var initData = { a:'', b:'' } var oldData = { a:'ww', b:'ee' } var newData ...

  6. ASP.NET MVC中,动态处理页面静态化

    首先解释一下什么是动态处理页面静态化 对于需要静态化的页面,第一次访问某个Action时,会先执行Action,并在页面渲染后向Response和服务器中网站的目录下都写入需要返回的html,而第二次 ...

  7. python中filter,reduce,map的用法

    filter的用法: 操作表list的内嵌函数'filter' 需要一个函数与一个list它用这个函数来决定哪个项应该被放入过滤结果队列中遍历list中的每一个值,输入到这个函数中如果这个函数返回Tr ...

  8. 继承 in her it

    ''' in her it 继承 de rive 派生 python2 (经典类|新式类) python3 (新式类) 1. What is inheritance? 什么是继承? 继承是一种新建类的 ...

  9. JAVA对mongodb的基本操作

    public class test3 { //连接数据库(不需要验证,用于测试连接本地的mongodb) public static MongoDatabase getDatabase(String ...

  10. Classnotfoundexception 与 noClassDelfaultError的区别

    ClassNotFoundException 这个异常特别常见,就是class找不到异常,一般的问题就是: 1 调用class的forName方法时,找不到指定的类 2 ClassLoader 中的 ...