当前位置:网站首页>Leetcode daily question (947. most stones removed with same row or column)
Leetcode daily question (947. most stones removed with same row or column)
2022-07-18 23:06:00 【wangjun861205】
On a 2D plane, we place n stones at some integer coordinate points. Each coordinate point may have at most one stone.
A stone can be removed if it shares either the same row or the same column as another stone that has not been removed.
Given an array stones of length n where stones[i] = [xi, yi] represents the location of the ith stone, return the largest possible number of stones that can be removed.
Example 1:
Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
Output: 5
Explanation: One way to remove 5 stones is as follows:
- Remove stone [2,2] because it shares the same row as [2,1].
- Remove stone [2,1] because it shares the same column as [0,1].
- Remove stone [1,2] because it shares the same row as [1,0].
- Remove stone [1,0] because it shares the same column as [0,0].
- Remove stone [0,1] because it shares the same row as [0,0].
Stone [0,0] cannot be removed since it does not share a row/column with another stone still on the plane.
Example 2:
Input: stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]
Output: 3
Explanation: One way to make 3 moves is as follows:
- Remove stone [2,2] because it shares the same row as [2,0].
- Remove stone [2,0] because it shares the same column as [0,0].
- Remove stone [0,2] because it shares the same row as [0,0].
Stones [0,0] and [1,1] cannot be removed since they do not share a row/column with another stone still on the plane.
Example 3:
Input: stones = [[0,0]]
Output: 0
Explanation: [0,0] is the only stone on the plane, so you cannot remove it.
Constraints:
- 1 <= stones.length <= 1000
- 0 <= xi, yi <= 104
- No two stones are at the same coordinate point.
In fact, this is a graph problem , Find isolated subgraphs from the graph , At the end of each subgraph, there will be a point that cannot be eliminated , So the number of points that can be eliminated is the number of all points - Number of orphan subgraphs .
struct Solution;
use std::collections::{
HashMap, HashSet};
impl Solution {
fn dfs(
current: (usize, usize),
i: usize,
xs: &Vec<Vec<usize>>,
ys: &Vec<Vec<usize>>,
parents: &mut HashMap<(usize, usize), usize>,
visited: &mut HashSet<(usize, usize)>,
) {
if visited.contains(¤t) {
return;
}
visited.insert(current);
for x in &xs[current.1] {
Solution::dfs((*x, current.1), i, xs, ys, parents, visited);
}
for y in &ys[current.0] {
Solution::dfs((current.0, *y), i, xs, ys, parents, visited);
}
parents.insert(current, i);
}
pub fn remove_stones(stones: Vec<Vec<i32>>) -> i32 {
let mut xs = vec![Vec::new(); 10usize.pow(4) + 1];
let mut ys = vec![Vec::new(); 10usize.pow(4) + 1];
for stone in &stones {
ys[stone[0] as usize].push(stone[1] as usize);
xs[stone[1] as usize].push(stone[0] as usize);
}
let mut parents = HashMap::new();
for (i, stone) in stones.iter().enumerate() {
Solution::dfs(
(stone[0] as usize, stone[1] as usize),
i,
&xs,
&ys,
&mut parents,
&mut HashSet::new(),
);
}
let iso: HashSet<usize> = parents.values().map(|v| *v).collect();
(stones.len() - iso.len()) as i32
}
}
边栏推荐
- [paddleseg source code reading] about the trivial matter that the paddleseg model returns a list
- How to deal with time series event data? [doctoral thesis of Munich University of technology] neural time series point process (ntpp): continuous time event data modeling
- [C #] positive sequence, reverse sequence, maximum value, minimum value and average value
- Private domain operation is very popular. Is private domain operation suitable for all enterprises?
- lun的概念
- Modify server password
- 数据受限条件下的多模态处理技术综述
- Sklearn linear regression fitting first-order term function
- SIEMENS模块6DD1661-0AE1
- 三种常用时序数据库对比调研-InfluxDB、Prometheus、IotDB
猜你喜欢

BurpSuite工具响应结果中文乱码解决方法

【远程桌面】rustdesk开源的远程桌面,TeamViewer 和向日葵的替代品

C语言自定义类型:结构体,枚举,联合

三种常用时序数据库对比调研-InfluxDB、Prometheus、IotDB

Problem solving -- > online OJ (16)

C language custom types: structure, enumeration, union

416. Partition equal sum subset · knapsack problem · dynamic programming

关于产品 | 要怎么进行产品规划?

练习动画最好的方式:封面过渡

私域运营很火,私域运营是否适合所有企业?
随机推荐
华为交换机配置SSH登录
About products | how to plan products?
lun的概念
Generate multiple databases at the same time based on multiple data sources and zero code, add, delete, modify and check restful API interfaces - mysql, PostgreSQL, Oracle, Microsoft SQL server multip
Be diligent in chatting
2022 soft test network administrator preparation guide
[Ji Zhong] July 13, 2022 3058 Torchbearer
NPM installation tutorial
SQL Server 各种锁 NOLOCK、UPDLOCK、HOLDLOCK、READPAST
快速完全删除node_modules
错误: -source 1.6 中不支持 diamond 运算符的解决办法
记一次蚂蚁金服四面遭虐,面试水太深,过河的渡船你造好了吗?
Deepmind's latest 114 page report "emerging barter trade behavior in Multi-Agent Reinforcement Learning"
[basic service] [database] MySQL master-slave replication deployment and configuration
DeepMind最新114页报告《多智能体强化学习中的新兴易货贸易行为》
日志收集方案EFK
PMP每日一练 | 考试不迷路-7.16
Enjoy meta mode of creation mode
[paddleseg source code reading] about the trivial matter that the paddleseg model returns a list
Original Rexroth proportional valve 4wrba10w64-2x/g24n9z4/m