Problem solution · C++

Minimum Cost to Connect Two Groups of Points

Minimum Cost to Connect Two Groups of Points: a C++ solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Direct simulation
Source
walkccc LeetCode Solutions
Length
47 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Minimum Cost to Connect Two Groups of Points, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 47 lines of C++ from the credited upstream file 1595.cpp.
  • The implementation visibly relies on sequence storage.
  • 4 loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMinimum Cost to Connect Two Groups of Points · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  int connectTwoGroups(vector<vector<int>>& cost) {    const int m = cost.size();    const int n = cost[0].size();    vector<vector<int>> mem(m, vector<int>(1 << n, INT_MAX));    // minCosts[j] := the minimum cost of connecting group2's point j    vector<int> minCosts(n);     for (int j = 0; j < n; ++j) {      int minCostIndex = 0;      for (int i = 1; i < m; ++i)        if (cost[i][j] < cost[minCostIndex][j])          minCostIndex = i;      minCosts[j] = cost[minCostIndex][j];    }     return connectTwoGroups(cost, 0, 0, minCosts, mem);  }  private:  // Returns the minimum cost to connect group1's points[i..n) with group2's  // points, where `mask` is the bitmask of the connected points in group2.  int connectTwoGroups(const vector<vector<int>>& cost, int i, int mask,                       const vector<int>& minCosts, vector<vector<int>>& mem) {    if (i == cost.size()) {      // All the points in group 1 are connected, so greedily assign the      // minimum cost for the unconnected points of group2.      int res = 0;      for (int j = 0; j < cost[0].size(); ++j)        if ((mask >> j & 1) == 0)          res += minCosts[j];      return res;    }    if (mem[i][mask] != INT_MAX)      return mem[i][mask];     for (int j = 0; j < cost[0].size(); ++j)      mem[i][mask] =          min(mem[i][mask],              cost[i][j] +                  connectTwoGroups(cost, i + 1, mask | 1 << j, minCosts, mem));     return mem[i][mask];  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗