国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 947. Most Nodes Removed

Zachary / 593人閱讀

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

Solution
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

相關文章

  • [LeetCode] 545. Boundary of Binary Tree

    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...

    newtrek 評論0 收藏0
  • LeetCode 545. Boundary of Binary Tree 二叉樹邊界

    摘要:二叉樹邊界題意高頻題,必須熟練掌握。逆時針打印二叉樹邊界。解題思路根據觀察,我們發現當為左邊界時,也是左邊界當為左邊界時,為空,則也可以左邊界。先加入左邊,加入,然后得到兩個子樹加入,最后加入右邊界。 LeetCode 545. Boundary of Binary Tree 二叉樹邊界Given a binary tree, return the values of its boun...

    Astrian 評論0 收藏0
  • [LeetCode] 429. N-ary Tree Level Order Traversal (

    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...

    LiangJ 評論0 收藏0
  • [LeetCode] 684. Redundant Connection

    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...

    lncwwn 評論0 收藏0

發表評論

0條評論

Zachary

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<