Leetcode-editorials
Contribute
  • Leetcode question of the day
  • 08-August
    • 26. Maximum Length of Pair Chain
    • 28. Implement stack using queues
    • 29. Minimum Penalty for a Shop
    • 30. Minimum Replacements to Sort the Array
    • 31. Minimum Number of Taps to Open to Water a Garden
  • 09-September
    • 01. Counting Bits
    • 02. Extra Characters in a String
    • 03. Unique Paths
    • 04. Linked List Cycle
    • 05. Copy List with Random Pointer
    • 06. Split Linked List in Parts
    • 07. Reverse Linked List II
    • 08. Pascal's Triangle
    • 09. Combination Sum IV
    • 10. Count All Valid Pickup and Delivery Options
    • 11. Group the People Given the Group Size They Belong To
    • 12. Minimum Deletions to Make Character Frequencies Unique
    • 13. Candy
    • 14. Reconstruct Itinerary
    • 15. Min Cost to Connect All Points
    • 16. Path With Minimum Effort
    • 17. Shortest Path Visiting All Nodes
    • 18. The K Weakest Rows in a Matrix
    • 19. Find the Duplicate Number
    • 20. Minimum Operations to Reduce X to Zero
    • 21-Median of Two Sorted Arrays
    • 22- Is Subsequence
    • 23- Longest String Chain
    • 24- Champagne Tower
    • 25- Find the Difference
    • 26- Remove Duplicate Letters
    • 27- Decoded String at Index
    • 28- Sort Array By Parity
    • 29- Monotonic Array
    • 30- 132 Pattern
  • 10-October
    • 01. Reverse Words in a String III
    • 02. Remove Colored Pieces if Both Neighbors are the Same Color
    • 03. Number of Good Pairs
    • 04. Design HashMap
    • 05. Majority Element II
    • 06. Integer Break
    • 07. Build Array Where You Can Find The Maximum Exactly K Comparisons
  • 11-November
    • 01. Find Mode in Binary Search Tree
    • 02. Count Nodes Equal to Average of Subtree
    • 03. Build an Array With Stack Operations
    • 04. Last Moment Before All Ants Fall Out of a Plank
    • 07. Eliminate Maximum Number of Monsters
  • Leetcode Contests
    • Weekly Contest
      • Weekly-Contest-360
Powered by GitBook
On this page
  • My Approach
  • Time and Auxiliary Space Complexity
  • Code (C++)
  • Contribution and Support

Was this helpful?

Edit on GitHub
  1. 09-September

15. Min Cost to Connect All Points

Previous14. Reconstruct ItineraryNext16. Path With Minimum Effort

Last updated 1 year ago

Was this helpful?

The problem can be found at the following link:

My Approach

The problem is a classic example of the Minimum Spanning Tree (MST) problem. Given a set of points in a 2D plane, you want to find the minimum cost to connect all points such that there is exactly one simple path between any two points. The cost of connecting two points is defined as the Manhattan distance between them.

The provided solution is an implementation of Kruskal's algorithm, which is a popular algorithm for finding the MST of a graph. Here's an explanation of the solution step by step:

  1. Two helper functions are defined: find(parent, x): This function is used to find the representative (root) of a set using path compression. It recursively follows the parent pointers until it reaches the root of the set. Path compression is used to optimize the find operation by updating the parent pointers of all nodes along the path to the root, making future finds faster.

union(parent, x, y): This function is used to unite two sets by setting one's root as the parent of the other's root. It uses the find function to find the roots of the sets containing elements x and y and then sets the root of x as the parent of the root of y. This effectively merges the two sets.

  1. The number of points n is determined based on the input list points.

  2. A list called edges is created to store the edges between points along with their Manhattan distances. This list comprehensively computes the distances between all pairs of points and stores them in the form of tuples (distance, i, j) where distance is the Manhattan distance between point i and point j. Note that it only stores edges between distinct points (i.e., i is less than j).

  3. The edges list is sorted in ascending order based on their distances. This is done to consider the shortest edges first during the MST construction.

  4. A parent list is created, initialized such that each point is its own parent, effectively isolating all points at the beginning.

  5. Two variables, min_cost (initialized to 0) and num_edges (initialized to 0), are used to keep track of the total minimum cost and the number of edges added to the MST, respectively.

  6. The algorithm iterates through the sorted edges. For each edge (cost, u, v), it checks if adding this edge doesn't create a cycle in the minimum spanning tree. This is done by checking if find(parent, u) is not equal to find(parent, v), meaning that u and v belong to different sets.

  7. If the edge doesn't create a cycle, it unites the sets containing u and v using the union function, updates min_cost by adding the cost of the current edge, and increments num_edges.

  8. The loop continues until num_edges equals n - 1, which means that you have added enough edges to create a spanning tree that connects all n points.

  9. Finally, the function returns min_cost, which represents the minimum cost to connect all points in the MST.

Time and Auxiliary Space Complexity

  • Time Complexity: O(ElogE)

  • Auxiliary Space Complexity: O(ElogE)

Code (C++)


class Solution {
public:
    int minCostConnectPoints(vector<vector<int>>& points) {
        vector<int> parent;
        vector<pair<int, pair<int, int>>> edges;
        int n = points.size();

        for (int i = 0; i < n; ++i) {
            parent.push_back(i);
        }

        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                int cost = abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1]);
                edges.push_back({cost, {i, j}});
            }
        }

        sort(edges.begin(), edges.end());

        int min_cost = 0, num_edges = 0;

        for (auto& edge : edges) {
            int cost = edge.first;
            int u = edge.second.first;
            int v = edge.second.second;

            if (find(parent, u) != find(parent, v)) {
                unionSets(parent, u, v);
                min_cost += cost;
                num_edges++;
            }

            if (num_edges == n - 1) {
                break;
            }
        }

        return min_cost;
    }

private:
    int find(vector<int>& parent, int x) {
        if (parent[x] == x) {
            return x;
        }
        parent[x] = find(parent, parent[x]);
        return parent[x];
    }

    void unionSets(vector<int>& parent, int x, int y) {
        parent[find(parent, x)] = find(parent, y);
    }
};

Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the repository.

Question Link
discussion section
rishabhv12/Daily-Leetcode-Solution