[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 number of moves we can make?

Example 1:

Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
Output: 5

Example 2:

Input: stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]
Output: 3

Example 3:

Input: stones = [[0,0]]
Output: 0

Note:

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


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

现在,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


1436ms

 1 class Solution {
 2     func removeStones(_ stones: [[Int]]) -> Int {
 3         var n:Int = stones.count
 4         var ds:DJSet = DJSet(n)
 5         for i in 0..<n
 6         {
 7             for j in (i + 1)..<n
 8             {
 9                 if stones[i][0] == stones[j][0] || stones[i][1] == stones[j][1]
10                 {
11                     ds.union(i, j)
12                 }
13             }
14         }
15         return n-ds.count()
16     }
17 }
18
19 public class DJSet
20 {
21     var upper:[Int] = [Int]()
22
23     public init(_ n:Int)
24     {
25         upper = [Int](repeating:-1,count: n)
26     }
27
28     public func root(_ x:Int) -> Int
29     {
30         if upper[x] < 0
31         {
32             return x
33         }
34         else
35         {
36             upper[x] = root(upper[x])
37             return upper[x]
38         }
39     }
40
41     public func equiv(_ x:Int,_ y:Int) -> Bool
42     {
43         return root(x) == root(y)
44     }
45
46     public func union(_ x:Int,_ y:Int) -> Bool
47     {
48         var x:Int = root(x)
49         var y:Int = root(y)
50         if (x != y)
51         {
52             if (upper[y] < upper[x])
53             {
54                 var d:Int = x
55                 x = y
56                 y = d
57             }
58             upper[x] += upper[y]
59             upper[y] = x
60         }
61         return x == y
62     }
63
64     public func count()-> Int
65     {
66         var ct:Int = 0
67         for u in upper
68         {
69             if u < 0
70             {
71                 ct += 1
72             }
73         }
74         return ct
75     }
76 }

原文地址:https://www.cnblogs.com/strengthen/p/10015292.html

时间: 2024-10-04 08:58:41

[Swift Weekly Contest 112]LeetCode947. 移除最多的同行或同列石头 | Most Stones Removed with Same Row or Column的相关文章

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 提示:

[Swift Weekly Contest 112]LeetCode948. 令牌放置 | Bag of Tokens

You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be used at most once, has a value token[i], and has potentially two ways to use it. If we have at least token[i] power, we may play the token face up, losi

[Swift Weekly Contest 108]LeetCode931. 下降路径最小和 | Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t

[Swift Weekly Contest 109]LeetCode933. 最近的请求次数 | Number of Recent Calls

Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t represents some time in milliseconds. Return the number of pings that have been made from 3000 milliseconds ago until now. Any ping with time in [t - 3

[Swift Weekly Contest 109]LeetCode936. 戳印序列 | Stamping The Sequence

You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the s

[Swift Weekly Contest 113]LeetCode950. 按递增顺序显示卡牌 | Reveal Cards In Increasing Order

In a deck of cards, every card has a unique integer.  You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. Now, you do the following steps repeatedly, until all cards are revealed: Take the

[Swift Weekly Contest 113]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor

Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.

[Swift Weekly Contest 118]LeetCode970. 强整数 | Powerful Integers

Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0. Return a list of all powerful integers that have value less than or equal to bound. You may return the answer in any ord

[Swift Weekly Contest 120]LeetCode979. 在二叉树中分配硬币 | Distribute Coins in Binary Tree

Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins total. In one move, we may choose two adjacent nodes and move one coin from one node to another.  (The move may be from parent to child, or