Advertisement

Min Cost to Connect All Points - LeetCode 1584 Solution

Min Cost to Connect All Points - Complete Solution Guide

Min Cost to Connect All Points is LeetCode problem 1584, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an array points representing integer coordinates of some points on a 2D-plane, where points[i] = [x i , y i ] . The cost of connecting two points [x i , y i ] and [x j , y j ] is the manhattan distance between them: |x i - x j | + |y i - y j | , where |val| denotes the absolute value of val . Return the minimum cost to make all points connected. All points are connected if there is exactly one simple path between any two points. Example 1: Input: points = [[0,0],[2,2],[3,10],[5,2],

Detailed Explanation

The problem asks us to find the minimum cost to connect all points in a 2D plane. The input is an array of points, where each point is represented by its (x, y) coordinates. The cost of connecting two points is defined as the Manhattan distance between them, which is the sum of the absolute differences of their x and y coordinates: |x1 - x2| + |y1 - y2|. The goal is to find the minimum total cost such that there's exactly one simple path between any two points, meaning we need to find a Minimum Spanning Tree (MST).

Solution Approach

The provided code implements Prim's algorithm to find the MST. Instead of explicitly creating an adjacency list or matrix for the graph, it implicitly considers all possible edges between points based on their Manhattan distance. It maintains a `min_cost` array to store the minimum cost to connect each point to the MST being constructed and an `in_mst` array to indicate whether a point is already included in the MST. The algorithm iteratively adds the point with the minimum connection cost to the MST until all points are included.

Step-by-Step Algorithm

  1. Step 1: Initialize `min_cost` array with infinity for all points and `in_mst` array with `False` for all points. Set the `min_cost` of the starting point (point 0) to 0.
  2. Step 2: Initialize `total_cost` to 0 and `mst_size` to 0.
  3. Step 3: While the `mst_size` is less than the total number of points `n`:
  4. Step 4: Find the vertex `u` that is not yet in the MST (`in_mst[u] == False`) and has the minimum `min_cost[u]`.
  5. Step 5: Add the cost to connect `u` to the MST to `total_cost` and mark `u` as being in the MST (`in_mst[u] = True`). Increment `mst_size`.
  6. Step 6: For each vertex `v` that is not yet in the MST:
  7. Step 7: Calculate the Manhattan distance between `u` and `v`.
  8. Step 8: If the calculated distance is less than the current `min_cost[v]`, update `min_cost[v]` with the new distance.
  9. Step 9: Return `total_cost`.

Key Insights

  • Insight 1: The problem is essentially asking us to find the Minimum Spanning Tree (MST) of a complete graph where the vertices are the given points and the edges are weighted by the Manhattan distance between the corresponding points.
  • Insight 2: Prim's algorithm is a suitable algorithm for finding the MST, especially since the graph is implicitly complete (every point is connected to every other point). We can adapt Prim's algorithm to work with the given point data without explicitly constructing the graph.
  • Insight 3: We can optimize the algorithm by using a `min_cost` array to keep track of the minimum distance to connect each point to the growing MST. This avoids redundant distance calculations.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n)

Topics

This problem involves: Array, Union Find, Graph, Minimum Spanning Tree.

Companies

Asked at: DE Shaw, Directi, Nutanix.