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]); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/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...
閱讀 3371·2021-11-22 09:34
閱讀 2857·2021-10-09 09:43
閱讀 1445·2021-09-24 09:47
閱讀 2199·2019-08-30 12:53
閱讀 998·2019-08-29 14:00
閱讀 3356·2019-08-29 13:17
閱讀 2269·2019-08-28 18:00
閱讀 1284·2019-08-26 12:00