leetcode 947. Most Stones Removed with Same Row or Column(去掉同行列最多的石头)

PCIe 6.0实战指南:从链路训练到信号完整性的避坑手册 本文是一份针对PCIe 6.0硬件工程师的实战避坑指南。文章深入解析了从NRZ到PAM4调制带来的信号完整性挑战,详细拆解了链路训练(LTSSM)的复杂过程与调试方法,并提供了从信道仿真到实验室实测(如使用Keysight示波器进行眼图与误码率分析)的完整信号完整性闭环验证策略,帮助工程师解决高速率下的链路不稳定与降速问题。 阅读详情

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:

  1. Remove stone [2,2] because it shares the same row as [2,1].
  2. Remove stone [2,1] because it shares the same column as [0,1].
  3. Remove stone [1,2] because it shares the same row as [1,0].
  4. Remove stone [1,0] because it shares the same column as [0,0].
  5. 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.

Constraints:

1 <= stones.length <= 1000
0 <= xi, yi <= 104
No two stones are at the same coordinate point.

给一个2D数组,其中的元素代表2D的位置,同一位置只会有一个石头,同行或同列的石头能被移走(前提是存在与它同行或同列的石头,如果只有它自己就不能移走),问最多可移走多少个石头。

思路:
问题转换:同行同列的石头阵可以被移走直到只剩下一个石头。那么把同行同列的石头全都归到一个group,这个group的个数就是最后剩下的石头数量。则被移走的石头数量=石头总数量 - group个数

任务就变成了把同行同列的石头归到一个group里,两种方法,DFS和Union-Find

1) DFS
可把 (row, col) 看作一条边,建立无向图
但是注意一个问题,就是(1,2) 和 (2,1)是两个位置,但会被认为是同一条边,这时需要把这两种边区分开,题中有这样一个限制条件0 <= xi, yi <= 104 ,所以可定义0~10000是row的区间,10001~20001是col的范围。也就是把col + 10001,但是实际上col+10000也通过了。

这样做就可以访问一个位置(row,col)时,把row行和col列的位置全都标记为访问过,再递归标记它们关联的行和列,为一个组。下一次再从未访问过的位置重新标记新的一组。这样就可找出一共有多少个组。

可能行和列的概念容易混淆,可以认为列也是要遍历的“行”,只不过列的index是从10000开始的。

这里标记访问过不是一个位置一个位置地标,而是标整行整列,表示这一行或列已经访问过(可把列理解为index从10000开始的“行”)。

//19ms
    public int removeStones(int[][] stones) {
        int groups = 0;
        HashMap<Integer, List<Integer>> graph = new HashMap<>();
        boolean[] visited = new boolean[20001];
        
        for(int[] stone : stones) {
            int row = stone[0];
            int col = stone[1] + 10000;
            if(!graph.containsKey(row)) {
                graph.put(row, new ArrayList<Integer>());
            }
            graph.get(row).add(col);
            
            if(!graph.containsKey(col)) {
                graph.put(col, new ArrayList<Integer>());
            }
            graph.get(col).add(row);
        }
        
        for(Integer key : graph.keySet()) {
            if(visited[key]) continue;
            dfs(key, graph, visited);
            groups ++;
        }
        return stones.length - groups;
    }
    
    void dfs(int rowCol, HashMap<Integer, List<Integer>> graph, boolean[] visited) {
        if(visited[rowCol]) return;
        visited[rowCol] = true;
        //行的话找同一列,列的话找同一行的stone,把同行或同列的标记为访问,为同一group
        for(Integer next : graph.get(rowCol)) {
            if(visited[next]) continue;
            dfs(next, graph, visited);
        }
    }

如果DFS深度过深,担心StackOverflowError,可用stack版DFS,和BFS差不多,只不过BFS用的queue储存节点,先进先访问。DFS用stack储存节点,后进的先访问,直到到达尽头。

    public int removeStones(int[][] stones) {
        int groups = 0;
        HashMap<Integer, List<Integer>> graph = new HashMap<>();
        boolean[] visited = new boolean[20001];
        
        for(int[] stone : stones) {
            int row = stone[0];
            int col = stone[1] + 10000;
            if(!graph.containsKey(row)) {
                graph.put(row, new ArrayList<Integer>());
            }
            graph.get(row).add(col);
            
            if(!graph.containsKey(col)) {
                graph.put(col, new ArrayList<Integer>());
            }
            graph.get(col).add(row);
        }
        
        for(Integer key : graph.keySet()) {
            if(visited[key]) continue;
            Stack<Integer> stack = new Stack<>();
            stack.push(key);
            visited[key] = true;
            while(!stack.isEmpty()) {
                int rowCol = stack.pop();
                for(Integer next : graph.get(rowCol)) {
                    if(visited[next]) continue;
                    visited[next] = true;
                    stack.push(next);
                }
            }
            groups ++;
        }
        return stones.length - groups;
    }

2)Union-Find
思路和上面一样,也是把同行同列的归为一个group,最后用石头总数-group个数。

还是把(row, col)看成一条边,也可理解为它们是关联的数字。还是row是0~10000区间,col是10001~20001区间。但是这里加了一个情况,就是一个点还没有被访问的时候,它的parent是0,访问过的点要么parent是它自己,要么是其他点。所以为了和0区别开,row的范围移到1~10001,col移到10002~20002。
所以row要加1,col要加10002。

parent:上面还有其他parent。
root:parent是它自己(最上层parent)。

统一把col的root设为row的root。这样做有什么用处?row和col有同一个root,当同一列的位置来的时候,可通过col找到这个root,当同一行的位置来的时候,可通过row找到同一root,这样就可达到把同行和同列的石头都归为一个group的效果。

当row和col未访问过,即parent==0时,把row的parent标记为它自己,col的parent标记为row。一个访问过一个没访问过时,标记root为访问过的root。
当有不同的root时,把col的root,重点强调是root,而不是简单的parent,标记为row的root

//1ms~7ms
class Solution {
    int group = 0;
    public int removeStones(int[][] stones) {
        int[] root = new int[20003];
        for(int[] stone : stones) {
            union(root, stone[0] + 1, stone[1] + 10002);
        }
        return stones.length - group;
    }
    
    void union(int[] root, int row, int col) {
        int rootR = find(root, row);
        int rootC = find(root, col);
        
        //已经是一组(不需要处理),或者都没被访问过
        if(rootR == rootC) {
            if(rootR == 0) {  //都没被访问过,新增group
                group ++;
                root[row] = row;
                root[col] = row;
            }
        } else {  //一个没被访问过(设定root,group不变),或者行列不在一组(改root为一组,减去一个group)
            if(rootR == 0) {  //row没被访问过
                root[row] = rootC;
            } else if(rootC == 0) {
                root[col] = rootR;
            } else {
                root[rootC] = rootR; //从源头上改root,不要只改最下面节点
                group --;
            }
        }
    }
    
    int find(int[] root, int node) {
        int p = node;
        while(p != root[p]) {
            p = root[p];
            root[p] = root[root[p]];
        }
        return p;
    }
}
uniapp - H5网页打包运行报错Failed to load resource: net::ERR_FILE_NOT_FOUND(uniApp打包成H5部署到服务器,打包h5白屏并报错页面空白) Failed to load resource: net::ERR_FILE_NOT_FOUND,uniapp 坑之打包之后白屏,uniapp打包h5,HBuilderX 打包H5后空白报错 Failed to load resource: net::ERR FILE NOT_FOUND,UNI-APP H5应用白屏(空白页)解决方案,白屏并报错Failed to load resource: net::ERR_FILE_NOT_FOUND,uniapp开发,打包成H5部署到服务器,uniapp打包之后首页 阅读详情

相关推荐

C#程序突然崩溃怎么办?5分钟学会分析Dump文件(Visual Studio 2022实战)

本文详细介绍了在C#程序崩溃或无响应时,如何捕获并分析Dump文件以快速定位问题。通过Visual Studio 2022实战演示,重点讲解了配置符号服务器、解读调用堆栈与内存状态,以及针对CPU飙高、内存泄漏等典型场景的分析方法,帮助开发者高效进行故障诊断。

docker8compose的博客 483

Leetcode 947、移除最多的同行或同列石头

Leetcode 947、移除最多的同行或同列石头 并查集

hljjaaa的博客 218

自底向上(Bottom-Up)层次化综合脚本

的典型流程,核心逻辑是:先独立优化所有子模块,再通过顶层设计的约束反馈(表征)对子模块进行二次优化,最终确保子模块与顶层整合时满足全局时序和接口需求。,避免子模块 “各自为政” 导致的顶层时序违例。Example 73 通过 “子模块首次编译→顶层整合→表征反馈→子模块二次编译” 的流程,实现这一目标。这种流程兼顾了子模块的独立开发效率和顶层设计的全局一致性,是大型层次化芯片设计的常用方法。定义需要处理的子模块列表(E、D、C、B、A),后续流程将按此列表迭代处理每个子模块。Example 73 通过 “

weixin_45371279的博客 843

【LeetCode】947. Most Stones Removed with Same Row or Column 解题报告(Python & C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法并查集日期 题目地址:https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/description/ 题目描述 On a 2D plane, we place st...

负雪明烛 6122

PCIe-Orders set Blocks之symbol对齐

有序集的使用场合很多,如在链路link阶段,进入polling后,便开始有序集的发送。一个 Ordered Set Block 包含一个同步头,后跟一个有序集。多通道链路的所有 Lane 必须同时传输相同的有序集,而且有序集的第一个符号定义了有序集的类型。但在PCIE中,8GT/s及以上的速率,采用的是128b/130b编码。128-bit是想要传输的payload,实际会传输130-bit,多出来的2-bit就是sync header。Block有两种,通过Sync header区分:10b:表示data

weixin_44700185的博客 819

【2024-06-21】网易互娱秋招实习笔试三道编程题解

恭喜发现宝藏!搜索公众号【TechGuide】回复公司名,解锁更多新鲜好文和互联网大厂的笔经面经。作者@TechGuide【全网同名】

试问道几许 1060

LeetCode-947. 移除最多的同行或同列石头

题目链接:947. 移除最多的同行或同列石头 这道题也是看了题解,才知道是一道图论的题目(后悔没有好好学习离散数学)。做题的方法有很多种,最开始没用图论知识直接当成回溯问题去做直接超时,在自己电脑上跑一个规模是10的小样例一分半才出结果(鬼知道我是怎么等下去的,还好没有单步调试找断点)。如果把所有的石头建立成图,再去进行深度优先搜索找连通分量,能节省不少时间。不过由于最近复习了一下并查集,准备试试手。 顺便粘一下刚刚写的我对并查集的理解:数据结构——并查集 ...

qq_46032158的博客 288

宇树G1开发实践--PPT汇报(基础版)

本文介绍了一套基于宇树G1人形机器人的PPT自动演示系统,可实现"一键启动、自动演讲"功能。系统通过多线程同步技术,将音频播放与机器人动作完美结合,支持12种预设社交互动动作。硬件依赖宇树G1机器人、外接音响和网络连接,软件采用Python SDK和ALSA音频工具。核心功能包括音频文件自动排序、外接音响播放控制、机器人动作执行及音画同步。使用时需配置网络接口和音频设备号,运行后自动按顺序播放音频并执行随机动作。系统特别强调了安全注意事项,并提供了常见问题解决方案,适用于展会、汇报等需要

m0_47999117的博客 521

LeetCode 947 移除最多的同行或同列 HERODING的LeetCode之路

n 块石头放置在二维平面中的一些整数坐标点上。每个坐标点上最多只能有一块石头。 如果一块石头的 同行或者同列 上有其他石头存在,那么就可以移除这块石头。 给你一个长度为 n 的数组 stones ,其中 stones[i] = [xi, yi] 表示第 i 块石头的位置,返回 可以移除的石子 的最大数量。 示例 1: 输入:stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]] 输出:5 解释:一种移除 5 块石头的方法如下所示: 移除石头 [2,2] ,因为它和 [

HERODING23的博客 475

深度解构事务码 SQF:Support Query Framework 与 ABAP 静态分析实战

某制造企业在年度审计前,需要确认自定义发票过账程序 ZFI_INV* 系列是否存在硬编码公司代码与未捕获异常。技术团队在字段填入ZFI_INV*后,勾选与规则,执行扫描。SQF 生成的 ALV 报告列出 42 个警告,其中 17 条与抛出 CX_SY_NO_HANDLER 相关,8 条涉及动态 SQL 注入风险。结果导出至 Excel 供开发整改,并在 ATC 阶段验证修复。此过程较手工逐个执行 Code Inspector 节省超 80 % 时间。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 225

【从零到一】Jetson Nano上YOLOv5部署完全攻略:零基础小白也能玩转边缘AI推理

文章摘要: 本文详细介绍了在Jetson Nano边缘设备上部署YOLOv5目标检测模型的完整流程。从硬件准备、系统镜像烧录、基础环境配置,到CUDA/cuDNN验证、Python虚拟环境搭建及PyTorch安装,逐步指导开发者完成部署准备。教程兼顾理论原理与实践操作,提供Python和C++两种实现方案,并涵盖TensorRT优化、实时检测等生产级应用场景,帮助零基础开发者快速掌握边缘AI推理技术,实现高性能目标检测应用。

LXY_LOVE36的博客 376

LeetCode 947. 移除最多的同行或同列石头(并查集)

1. 题目 我们将石头放置在二维平面中的一些整数坐标点上。每个坐标点上最多只能有一块石头。 每次 move 操作都会移除一块所在行或者列上有其他石头存在的石头。 请你设计一个算法,计算最多能执行多少次 move 操作? 示例 1: 输入:stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]] 输出:5 示例 2: 输入:stones = [[0,0],[0,2],[1,1],[2,0],[2,2]] 输出:3 示例 3: 输入:stones = [[0,0]] 输出

Michael是个半路程序员 2081

大数据分析工具Power BI(二):Power BI下载安装和模块介绍

首先在Microsoft官方网站上下载PowerBI Desktop,然后进行安装。id=58494点击"Next"后会弹出下载框,进行下载即可,下载好的PowerBI名称为"PBIDesktopSetup_x64.exe",也可以在"资料"中获取安装包,双击安装包即可安装。

Lansonli(蓝深李)的博客 2347

LeetCode //C - 947. Most Stones Removed with Same Row or Column

This problem involves removing stones that share a row or column with another stone. The solution uses Union-Find (Disjoint Set Union) to group connected stones into components. The key observation is that in each component, all stones except one can be re

Made in Code 1523

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

LeetCode 947 Most Stones Removed with Same Row or Column (并查集)

On a 2D plane, we place stones at some integer coordinate points. Each coordinate point may have at most one stone. Now, amoveconsists of removing a stonethat shares a column or row with another ...

Tc的专栏 395

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

leetcode 947. Most Stones Removed with Same Row or Column

947. Most Stones Removed with Same Row or Column Find connected components, with some specific connected component, the remove count is Num of elements - 1. First method is to use BFS. class Solut...

weixin_30827565的博客 175

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

LeetCode周赛#112 Q3 Most Stones Removed with Same Row or Column(连通分量)

题目来源:https://leetcode.com/contest/weekly-contest-112/problems/most-stones-removed-with-same-row-or-column/ 问题描述  947. Most Stones Removed with Same Row or Column On a 2D plane, we place stones at s...

da_kao_la的博客 491

LeetCode 947. Most Stones Removed with Same Row or Column

On a 2D plane, we place stones at some integer coordinate points. Each coordinate point may have at most one stone. Now, a move consists of removing a stone that shares a column or row with another s...

katrina95的博客 285

[leetcode] 947. Most Stones Removed with Same Row or Column

Description 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 a

Learning from the mistakes 320

Leetcode | 947. Most Stones Removed with Same Row or Column [Union-Find/DPS]

Problem https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/ 分析 https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/197668/Count-the-Number-of-Islan...

mike_learns_to_rock的博客 519

LeetCode #947 - Most Stones Removed with Same Row or Column

题目描述: On a 2D plane, we place stones at some integer coordinate points.  Each coordinate point may have at most one stone. Now, a move consists of removing a stone that shares a column or row with a...

LawFile的博客 221

【LeetCode】947. Most Stones Removed with Same Row or Column 移除最多的同行或同列石头(Medium)(JAVA)每日一题

【LeetCode】947. Most Stones Removed with Same Row or Column 移除最多的同行或同列石头(Medium)(JAVA) 题目地址: https://leetcode.com/problems/minimum-increment-to-make-array-unique/ 题目描述: On a 2D plane, we place n stones at some integer coordinate points. Each coordinate poin

qq_16927853的博客 250

leetcode -- 947. Most Stones Removed with Same Row or Column

On a 2D plane, we place stones at some integer coordinate points.Each coordinate point may have at most one stone. Now, a move consists of removing a stonethat shares a column or row with another ...

猪の成长日记 258

LeetCode 947. Most Stones Removed with Same Row or Column 并查集

On a 2D plane, we placenstones at some integer coordinate points. Each coordinate point may have at most one stone. A stone can be removed if it shares eitherthe same row or the same columnas another stone that has not been removed. Given an arraysto...

taoqick的专栏 277

LeetCode—947. 移除最多的同行或同列石头(Most Stones Removed with Same Row or Column)——分析及代码(Java)

LeetCode—947. 移除最多的同行或同列石头[Most Stones Removed with Same Row or Column]——分析及代码[Java]一、题目二、分析及代码1. 并查集(1)思路(2)代码(3)结果三、其他 一、题目 n 块石头放置在二维平面中的一些整数坐标点上。每个坐标点上最多只能有一块石头。 如果一块石头的 同行或者同列 上有其他石头存在,那么就可以移除这块石头。 给你一个长度为 n 的数组 stones ,其中 stones[i] = [xi, yi] 表示第 i 块

江南土豆的博客 310

LeetCode 947. Most Stones Removed with Same Row or Column - 彻底掌握并查集(Union Find)系列题20

题目要我们尽可能多地从一个二维矩阵里移除石头,给的移除条件是,如果一颗石头所在的行或者列上有其他石头那么这颗石头就可以被移除。因此如果我们把具有相同行坐标或者相同列坐标的石头合并成一个集合,那么这个集合里石头就可以一颗一颗地被移除直到只剩下最后一颗。也就说最后每个集合都只剩下一颗石头,因此石头总数减去集合个数就是最多可移除的石头数。很显然这种问题用并查集UnionFind解答再适合不过。

hgq522的博客 346
上一篇: leetcode 938. Range Sum of BST(二叉搜索树范围内的和)
下一篇: leetcode 832. Flipping an Image(图像翻转)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址