Problem
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 <= stones.length <= 1000
0 <= stonesi < 10000
class Solution { public int removeStones(int[][] stones) { int m = stones.length; int[] parents = new int[m]; for (int i = 0; i < m; i++) { parents[i] = i; } int count = 0; for (int i = 0; i < m; i++) { for (int j = i+1; j < m; j++) { if (stones[j][0] == stones[i][0] || stones[j][1] == stones[i][1]) { int p1 = find(parents, i); int p2 = find(parents, j); parents[p1] = p2; if (p1 != p2) { count++; } } } } return count; } private int find(int[] parents, int i) { if (parents[i] == i) return i; else return find(parents, parents[i]); } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/72531.html
Problem Given a binary tree, return the values of its boundary in anti-clockwise direction starting from root. Boundary includes left boundary, leaves, and right boundary in order without duplicate no...
摘要:二叉树边界题意高频题,必须熟练掌握。逆时针打印二叉树边界。解题思路根据观察,我们发现当为左边界时,也是左边界当为左边界时,为空,则也可以左边界。先加入左边,加入,然后得到两个子树加入,最后加入右边界。 LeetCode 545. Boundary of Binary Tree 二叉树边界Given a binary tree, return the values of its boun...
429. N-ary Tree Level Order Traversal Given an n-ary tree, return the level order traversal of its nodes values. (ie, from left to right, level by level). For example, given a 3-ary tree:showImg(https...
Problem In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one...
阅读 3330·2021-11-22 09:34
阅读 2809·2021-10-09 09:43
阅读 1336·2021-09-24 09:47
阅读 2171·2019-08-30 12:53
阅读 954·2019-08-29 14:00
阅读 3322·2019-08-29 13:17
阅读 2236·2019-08-28 18:00
阅读 1252·2019-08-26 12:00