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

题目描述:

在二维平面上,我们将石头放置在一些整数坐标点上。每个坐标点上最多只能有一块石头。

现在,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]]
输出:0

提示:

  1. 1 <= stones.length <= 1000
  2. 0 <= stones[i][j] < 10000

思路分析:

题目理解起来实际就是求独立分量的个数。这类题就用并查集,一开始被两个坐标的比较限制,实际只要二者满足一个即可。利用一个数组来存每个位置的组号,满足同行同列就进行归并。初始所有的组号都为-1,最后统计剩余的-1个数,总数减去独立分量的个数即为所求。

代码:

 1 class Solution {
 2 public:
 3     int find(vector<int>&f, int x)
 4     {
 5         return f[x]==-1?x:find(f, f[x]);
 6     }
 7     void u(vector<int>&f, int i, int j)
 8     {
 9         int fx = find(f, i);
10         int fy = find(f, j);
11         if(fx!=fy)
12             f[fx] = fy;
13     }
14     int removeStones(vector<vector<int>>& stones) {
15         if(stones.size()==0)
16             return 0;
17         int n = stones.size();
18         vector<int> f(n, -1);
19         for(int i=0; i<n; i++)
20         {
21             for(int j=i+1; j<n; j++)
22             {
23                 if(stones[i][0]==stones[j][0]||stones[i][1]==stones[j][1])
24                 {
25                     u(f, i, j);
26                 }
27             }
28         }
29         int cnt=0;
30         for(int i=0; i<n; i++)
31         {
32             if(f[i]==-1)
33                 cnt++;
34         }
35         return n-cnt;
36     }
37 };

原文地址:https://www.cnblogs.com/LJ-LJ/p/11437625.html

时间: 2024-08-03 11:34:52

leetcode 947. 移除最多的同行或同列的石头的相关文章

[Swift Weekly Contest 112]LeetCode947. 移除最多的同行或同列石头 | 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 stone on the grid. What is the largest possible num

图解双指针 | LeetCode 27. 移除元素

题目描述 原题链接:LeetCode 27. 移除元素 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 元素的顺序可以改变.你不需要考虑数组中超出新长度后面的元素. 示例 1: 给定 nums = [3,2,2,3], val = 3, 函数应该返回新的长度 2, 并且 nums 的前两个元素均为 2. 你不需要考虑数组中超出新长度后面的元素

leetcode 4. 移除有序数组中的重复元素 Remove Duplicates from Sorted Array

问题:Remove Duplicates from Sorted Array II 难度:medium Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2

力扣(LeetCode)移除链表元素 个人题解

删除链表中等于给定值 val 的所有节点. 这题粗看并不困难,链表的特性让移除元素特别轻松,只用遇到和val相同的就跳过,将指针指向下一个,以此类推. 但是,一个比较麻烦的问题是,当链表所有元素都和val相同时,如果直接使用参数给的head,则返回的一定会保留第一位的节点,而题意是要返回空值. 对上述情况使用特判又会与"第一个节点的值和val不同,第二个节点之后和val值相同"相矛盾. 所以想到的思路是,新建一个节点,将这个节点接在head的最前面,保证第一个节点无意义,这样,哪怕遇到

Leetcode 546.移除盒子

移除盒子 给出一些不同颜色的盒子,盒子的颜色由数字表示,即不同的数字表示不同的颜色.你将经过若干轮操作去去掉盒子,直到所有的盒子都去掉为止.每一轮你可以移除具有相同颜色的连续 k 个盒子(k >= 1),这样一轮之后你将得到 k*k 个积分.当你将所有盒子都去掉之后,求你能获得的最大积分和. 示例 1:输入: [1, 3, 2, 2, 2, 3, 4, 3, 1] 输出: 23 解释: [1, 3, 2, 2, 2, 3, 4, 3, 1] ----> [1, 3, 3, 4, 3, 1] (

LeetCode 10.移除数组中与给定值相同的元素

题目描述 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 元素的顺序可以改变.你不需要考虑数组中超出新长度后面的元素. 示例 1: 给定 nums = [3,2,2,3], val = 3, 函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2. 你不需要考虑数组中超出新长度后面的元素. 示例 2: 给定 nums = [0,1

Leetcode 402. 移掉K位数字

// 贪心算法,当前遇到的数,比栈顶的元素小,就将栈顶元素给弹出,直至遇到比栈顶大或者是栈为空才停止,字符串保证是一个正常的数字序列.class Solution { public: string removeKdigits(string num, int k) { std::vector<int> S;//为了遍历,因此可以用vector来进行模拟. std::string result = "";// 保存最后的结果 for(int i=0; i<num.size

leetcode 203. 移除链表元素(Remove Linked List Elements)

目录 题目描述: 示例: 解法: 题目描述: 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4->5 解法: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), nex

leetcode 11盛水最多的容器

class Solution { public: int maxArea(vector<int>& height) { //双指针法:从最宽的容器开始计算,当更窄的容器盛水量要大于之前容器,那必须比之前容器高,因此可以移动两个指针,直到最窄time O(n),space O(1); int low=0; int high=height.size()-1; int volume=0; while(low<high){ int h=min(height[low],height[hig