成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 947. Most Nodes Removed

Zachary / 602人閱讀

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]);
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/72531.html

相關(guān)文章

  • [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 評(píng)論0 收藏0
  • LeetCode 545. Boundary of Binary Tree 二叉樹邊界

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

    Astrian 評(píng)論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 評(píng)論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 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<