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 ...
随机推荐
- net core 随笔
UseApplicationInsights 这个有用到azure 才有用, 平时没用的话可以去掉. 遥测. 上下文指的是 进程间占有的资源空间. 当一个进程时间片到了或者资缺的时候就会让出CPU ...
- Git实操
使用git首先要理解工作区(working).暂存区(stage或者index).和版本库(repo区),很多命令都是和这三个概念相关的. git init 初始化git仓库,会生成默认的.git文件 ...
- hosts 添加主机和ip映射
背景:在服务器搭建环境时,迁移项目后,停止服务出现以下问题. 原因:项目停止或启动找不到映射名称或服务. 解决:修改hosts文件.hosts文件地址 /etc/hosts 添加主机和 ...
- 启动ssh服务 XSshell 生成秘钥 并注册公钥在Ubuntu linux
安装ssh服务:sudo apt-get install openssh-server 查看ssh服务: ps -ef | grep ssh 查看之后正常显示如下3行:root 8 ...
- SQL优化清单
SQL优化清单 1.from 语句中包含多个表的情况下,把记录数少的表放在前面 2.where 语句中包含多个条件时,将刷选多的条件放前面 3.避免使用select * ,因为这样会去查询所有列的数据 ...
- 第2次作业 -- 熟悉 JUnit 测试
2.1 Mooctest 使用心得 Mooctest很方便,可以即时测评自己写的测试代码,获得覆盖率和报告,不需要自己安装配置环境 而且安装配置插件的环境也很简单,可以专注于测试本身 2.2 Juni ...
- ABP异常处理
1.编译器错误消息: CS0012: 类型“System.Object”在未被引用的程序集中定义.必须添加对程序集“System.Runtime, Version=4.0.0.0, Culture=n ...
- jQuery 命名空间的使用
jQuery 命名空间的使用: 1.利用 trigger 触发子元素带命名空间的事件,那么父元素带相同命名空间的事件也会被触发,而父元素没有命名空间的事件不会被触发. 2.利用 trigger 触发子 ...
- 初读"Thinking in Java"读书笔记之第八章 --- 多态
再论向上转型 在某些方法中,仅接收基类作为参数,而不是特殊的导出类,会使得程序更容易扩展. 转机 方法调用绑定 前期绑定(编译期绑定):在编译期将一个方法调用和方法主体关联起来. 后期绑定(动态绑定或 ...
- 专业的“python爬虫工程师”需要学习哪些知识?
学到哪种程度 暂且把目标定位初级爬虫工程师,简单列一下吧: (必要部分) 熟悉多线程编程.网络编程.HTTP协议相关 开发过完整爬虫项目(最好有全站爬虫经验,这个下面会说到) 反爬相关,cookie. ...