1. 假设有一个棋盘(二维坐标系), 棋盘上摆放了一些石子(每个石子的坐标都为整数). 你可以remove一个石子, 当且仅当这个石子的同行或者同列还有其它石子. 输入是一个list of points.
  2.  
  3. 问:
  4. 1) 给这些石子坐标, 你最多能remove多少个石子?
  5. 2) Follow-up: 若想保证remove的石子数量最大, 应按照什么顺序remove? (没有写代码)

DFS count connected components:

  1. // "static void main" must be defined in a public class.
  2. public class Main {
  3. public static void main(String[] args) {
  4. int[] a = {0,0};
  5. int[] b = {0,1};
  6. int[] c = {1,2};
  7. int[] d = {2,3};
  8. List<int[]> coords = new ArrayList<>();
  9. coords.add(a);
  10. coords.add(b);
  11. coords.add(c);
  12. coords.add(d);
  13.  
  14. System.out.println(new Solution().minReminingChessPieces(coords));
  15. }
  16. }
  17. class Solution{
  18.  
  19. public int minReminingChessPieces(List<int[]> coords){
  20. if(coords == null){
  21. return -1;
  22. }
  23. HashSet<String> visited = new HashSet<>();
  24. int res = 0;
  25. for(int[] coord : coords){
  26. String s = coord[0]+":"+coord[1];
  27. if(!visited.contains(s)){
  28. res++;
  29. DFSHelper(coords, coord, visited);
  30. }
  31. }
  32. return res;
  33. }
  34.  
  35. public void DFSHelper(List<int[]> coords, int[] coord, HashSet<String> visited){
  36. visited.add(coord[0]+":"+coord[1]);
  37. for(int[] subCoord : coords){
  38. if(subCoord[0] == coord[0] || subCoord[1] == coord[1]){
  39. if(!visited.contains(subCoord[0]+":"+subCoord[1])){
  40. DFSHelper(coords, subCoord, visited);
  41. }
  42. }
  43. }
  44. }
  45.  
  46. }

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. net core 随笔

    UseApplicationInsights  这个有用到azure 才有用, 平时没用的话可以去掉. 遥测. 上下文指的是 进程间占有的资源空间. 当一个进程时间片到了或者资缺的时候就会让出CPU ...

  2. Git实操

    使用git首先要理解工作区(working).暂存区(stage或者index).和版本库(repo区),很多命令都是和这三个概念相关的. git init 初始化git仓库,会生成默认的.git文件 ...

  3. hosts 添加主机和ip映射

    背景:在服务器搭建环境时,迁移项目后,停止服务出现以下问题. 原因:项目停止或启动找不到映射名称或服务. 解决:修改hosts文件.hosts文件地址 /etc/hosts         添加主机和 ...

  4. 启动ssh服务 XSshell 生成秘钥 并注册公钥在Ubuntu linux

    安装ssh服务:sudo apt-get install openssh-server 查看ssh服务:    ps -ef | grep ssh 查看之后正常显示如下3行:root        8 ...

  5. SQL优化清单

    SQL优化清单 1.from 语句中包含多个表的情况下,把记录数少的表放在前面 2.where 语句中包含多个条件时,将刷选多的条件放前面 3.避免使用select * ,因为这样会去查询所有列的数据 ...

  6. 第2次作业 -- 熟悉 JUnit 测试

    2.1 Mooctest 使用心得 Mooctest很方便,可以即时测评自己写的测试代码,获得覆盖率和报告,不需要自己安装配置环境 而且安装配置插件的环境也很简单,可以专注于测试本身 2.2 Juni ...

  7. ABP异常处理

    1.编译器错误消息: CS0012: 类型“System.Object”在未被引用的程序集中定义.必须添加对程序集“System.Runtime, Version=4.0.0.0, Culture=n ...

  8. jQuery 命名空间的使用

    jQuery 命名空间的使用: 1.利用 trigger 触发子元素带命名空间的事件,那么父元素带相同命名空间的事件也会被触发,而父元素没有命名空间的事件不会被触发. 2.利用 trigger 触发子 ...

  9. 初读"Thinking in Java"读书笔记之第八章 --- 多态

    再论向上转型 在某些方法中,仅接收基类作为参数,而不是特殊的导出类,会使得程序更容易扩展. 转机 方法调用绑定 前期绑定(编译期绑定):在编译期将一个方法调用和方法主体关联起来. 后期绑定(动态绑定或 ...

  10. 专业的“python爬虫工程师”需要学习哪些知识?

    学到哪种程度 暂且把目标定位初级爬虫工程师,简单列一下吧: (必要部分) 熟悉多线程编程.网络编程.HTTP协议相关 开发过完整爬虫项目(最好有全站爬虫经验,这个下面会说到) 反爬相关,cookie. ...