23. Maximum Stone Removal
β GFG solution to the Maximum Stone Removal problem: find maximum number of removable stones sharing rows or columns using Union-Find and graph connectivity techniques. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given a 2D array stones[][] of non-negative integers where stones[i] = [xi, yi] represents the location of the ith stone on a 2D plane. Your task is to return the maximum possible number of stones that you can remove.
A stone can be removed if it shares either the same row or the same column as another stone that has not been removed. The goal is to maximize the number of stones removed while following this constraint.
Note: Each coordinate point may have at most one stone.
π Examples
Example 1
Input: stones[][] = [[0, 0], [0, 1], [1, 0], [1, 2], [2, 1], [2, 2]]
Output: 5
Explanation: One way to remove 5 stones:
1. Remove [2, 2] (shares row with [2, 1])
2. Remove [2, 1] (shares column with [0, 1])
3. Remove [1, 2] (shares row with [1, 0])
4. Remove [1, 0] (shares column with [0, 0])
5. Remove [0, 1] (shares row with [0, 0])
Stone [0, 0] remains as it cannot be removed.Example 2
π Constraints
$1 \le \text{stones.size()} \le 1000$
$0 \le x_i, y_i \le 10^4$
No two stones are at the same position
β
My Approach
The optimal approach uses Union-Find (Disjoint Set Union) to group stones into connected components based on shared rows or columns:
Union-Find (DSU) Strategy
Component Identification:
Stones sharing the same row or column belong to the same connected component.
Within each component, all stones except one can be removed.
Coordinate Mapping:
Map each row index to a unique identifier.
Map each column index to a different unique identifier (offset by 10001 to avoid collision).
Union row and column identifiers for each stone.
Path Compression:
Implement find operation with path compression for O(Ξ±(n)) amortized time.
Union operation merges components by connecting their root parents.
Count Components:
After processing all stones, count distinct connected components.
Maximum removable stones = Total stones - Number of components.
Key Insight:
Each connected component must keep at least one stone.
All other stones in that component can be removed.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n Γ Ξ±(n)), where n is the number of stones and Ξ±(n) is the inverse Ackermann function, which is nearly constant for all practical values. Each union and find operation takes O(Ξ±(n)) time with path compression.
Expected Auxiliary Space Complexity: O(n), as we use a parent array of size 20002 to handle coordinate mapping and a hash set to store unique component roots.
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated