LeetCode-947. Remove the most peers or stones in the same column

description

n stones are placed on some integer coordinate points in a two-dimensional plane. There can be at most one stone on each coordinate point.

If there are other stones in the same row or in the same row of a stone, then the stone can be removed.

Give you an array stones of length n, where stones[i] = [xi, yi] represents the position of the i-th stone, and returns the maximum number of stones that can be removed.

 

Example 1:

Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
Output: 5
Explanation: A removal The method of 5 stones is as follows:
1. Remove stone [2,2] because it is the same as [2,1].
2. Remove stone [2,1] because it is in the same column as [0,1].
3. Remove the stone [1,2] because it goes with [1,0].
4. Remove the stone [1,0] because it is in the same column as [0,0].
5. Remove stone [0,1] because it is the same as [0,0].
The stone [0,0] cannot be removed because it is not in line/column with another stone.
Example 2:

Input: stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]
Output: 3
Explanation: A method to remove 3 stones is as follows Shown:
1. Remove stone [2,2] because it is the same as [2,0].
2. Remove stone [2,0] because it is in the same column as [0,0].
3. Remove stone [0,2] because it is the same as [0,0].
The stones [0,0] and [1,1] cannot be removed because they are not in line/column with another stone.
Example 3:

Input: stones = [[0,0]]
Output: 0
Explanation: [0,0] is the only stone on the plane, so it cannot be removed.

Source: LeetCode
Link: https://leetcode-cn.com/problems/most-stones-removed-with-same-row-or-column/

Solve

    // 使用散列表实现一个动态增加的并查集
    class VarUnionFind {
    private:
        unordered_map<int, int> parent;
        unordered_map<int, int> rank;
    public:
        int find(int p) {
            if (parent.count(p) == 0) {
                // 如果p在散列表中不存在,则添加进去
                parent[p] = p;
                return p;
            }
            while (p != parent[p]) {
                parent[p] = parent[parent[p]];
                p = parent[p];
            }
            return p;
        }

        void unionElements(int p, int q) {
            int pRoot = find(p);
            int qRoot = find(q);
            if (pRoot == qRoot) {
                return;
            }
            if (rank[pRoot] < rank[qRoot]) {
                parent[pRoot] = qRoot;
                return;
            }
            if (rank[pRoot] > rank[qRoot]) {
                parent[qRoot] = pRoot;
                return;
            }
            // rank[pRoot] == rank[qRoot]
            parent[pRoot] = qRoot;
            ++rank[qRoot];
        }

        // 返回并查集的连通分量
        int getConnectedComponent() {
            int count = 0;
            for (auto &[p, root] : parent) {
                if (p == root) {
                    ++count;
                }
            }
            return count;
        }
    };

    class Solution {
    public:
        // 方法一,使用并查集,求取连通分量,通过分析可知,每个连通分量通过不停的粉碎可以只剩下一块石头
        // 则结果为n - 连通分量个数
        int removeStones_useUnionFind(vector<vector<int>> &stones) {
            const int OFFSET = 100001; // 纵坐标的偏移量,为了不跟横坐标重合的冲突
            VarUnionFind uf;
            for (auto &vec : stones) {
                uf.unionElements(vec[0], vec[1] + OFFSET);
            }
            // 石头数量减去并查集的连通分量即为可以粉碎的最大数量石头
            return stones.size() - uf.getConnectedComponent();
        }

        // 方法二,深度遍历图得到连通分量,这里将每块石头看做图的一个结点,通过观察是否在同行或者同列来确定是否有边相连,
        // 最后通过深度遍历计算连通分量,则结果为n - 连通分量个数
        int removeStones_useDFS1e(vector<vector<int>> &stones) {
            const int n = stones.size();
            vector<vector<int>> edges(n, vector<int>());  // 邻接表存储图
            // 构造图
            for (int i = 0; i < n; ++i) {
                for (int j = i + 1; j < n; j++) {
                    if (stones[i][0] == stones[j][0] || stones[i][1] == stones[j][1]) {
                        edges[i].push_back(j);
                        edges[j].push_back(i);  // 构造一个无向图
                    }
                }
            }

            // 深度遍历,求取连通分量
            int count = 0;  // 连通分量初始化为0
            visited.assign(n, false); // 结点访问标记初始化
            for (int i = 0; i < n; ++i) {
                if (!visited[i]) {
                    ++count;
                    dfs(edges, i);
                }
            }
            return n - count;
        }

        // 方法三,方法二建图部分时间复杂度为O(N*N),进行优化,将同行或者同列的石头放一起,再进行建图
        int removeStones(vector<vector<int>> &stones) {
            const int n = stones.size();
            const int OFFSET = 100001; // 纵坐标的偏移量,为了不跟横坐标重合的冲突
            unordered_map<int, vector<int>> record;
            // 计算每个行或者列上有哪些石头(图节点)
            for (int i = 0; i < n; ++i) {
                record[stones[i][0]].emplace_back(i);  // 在横坐标stones[i][0]上有石头i(即图节点i )
                record[stones[i][1] + OFFSET].emplace_back(i);  // 在横坐标stones[i][1] + OFFSET上有石头i(即图节点i )
            }

            // 构造图
            vector<vector<int>> edges(n, vector<int>());  // 邻接表构造图
            for (auto &[_, vec] : record) {
                int k = vec.size();
                for (int i = 1; i < k; ++i) {
                    // 在同行或者同列的节点肯定相连,注意无向图的处理
                    edges[vec[i - 1]].emplace_back(vec[i]);
                    edges[vec[i]].emplace_back(vec[i - 1]);
                }
            }

            // 深度遍历,求取连通分量
            int count = 0;  // 连通分量初始化为0
            visited.assign(n, false); // 结点访问标记初始化
            for (int i = 0; i < n; ++i) {
                if (!visited[i]) {
                    ++count;
                    dfs(edges, i);
                }
            }
            return n - count;
        }

    private:
        // 图的深度遍历
        void dfs(const vector<vector<int>> &edges, int v) {
            visited[v] = true;
            const auto &adjPoints = edges[v];
            for (auto w : adjPoints) {
                if (!visited[w]) {
                    dfs(edges, w);
                }
            }
        }

        // 图结点是否访问标志,用于辅助遍历
        vector<bool> visited;
    };

 

Guess you like

Origin blog.csdn.net/u010323563/article/details/112709782