Problem solution · C++

Minimum Time to Transport All Individuals

Minimum Time to Transport All Individuals: a C++ solution using heap or priority queue. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Heap or priority queue
Source
Kamyu LeetCode Solutions
Length
65 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

Heap or priority queue

For Minimum Time to Transport All Individuals, the implementation repeatedly takes the currently best candidate from a heap while inserting newly available choices.

  1. Define the priority key and whether the smallest or largest item should lead.
  2. Push each candidate when it becomes eligible.
  3. Discard stale entries when necessary and process the best live candidate.

Code notes

  • 65 lines of C++ from the credited upstream file minimum-time-to-transport-all-individuals.cpp.
  • The implementation visibly relies on sequence storage, work queue.
  • 5 loop blocks detected.

Complexity

Count heap pushes and pops; each normally contributes a logarithmic factor in the heap size.

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

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMinimum Time to Transport All Individuals · C++C++
Use this to learn the idea, then write your own version.
// Time:  O((n * m * 2^n + m * 3^n) * log(n * m * 2^n + m * 3^n)) = O(m * 3^n * log(m * 3^n))// Space: O(n * m * 2^n + m * 3^n) = O(m * 3^n) // dijkstra's algorithm, submask enumerationclass Solution {public:    double minTime(int n, int k, int m, vector<int>& time, vector<double>& mul) {        static const auto INF = numeric_limits<double>::max();         vector<int> lookup(1 << n);        for (int mask = 1; mask < 1 << n; ++mask) {  // Time: O(n * 2^n)            for (int i = 0; i < n; ++i) {                if (!(mask & (1 <<i))) {                    continue;                }                lookup[mask] = max(lookup[mask], time[i]);            }        }                vector<vector<vector<double>>> dist(2, vector<vector<double>>(m, vector<double>(1 << n, INF)));        dist[0][0][(1 << n) - 1] = 0.0;        using D = tuple<double, int, int, int>;        priority_queue<D, vector<D>, greater<D>> min_heap;        min_heap.emplace(0.0, 0, 0, (1 << n) - 1);        const auto& update = [&](double d, int r, int s, int mask, int submask) {            const auto& t = lookup[submask] * mul[s];            const auto& nr = r ^ 1;            const auto& ns = (s + static_cast<int>(t)) % m;            const auto& new_mask = mask ^ submask;            const auto& nd = d + t;            if (dist[nr][ns][new_mask] > nd) {                dist[nr][ns][new_mask] = nd;                min_heap.emplace(nd, nr, ns, new_mask);            }        };         while (!empty(min_heap)) {            const auto [d, r, s, mask] = min_heap.top(); min_heap.pop();  // Total Time: O((n * m * 2^n + m * 3^n) * log(n * m * 2^n + m * 3^n))            if (d != dist[r][s][mask]) {                continue;            }            if (mask == 0) {                assert(r == 1);                return d;            }            if (r == 0) {                for (int submask = mask; submask; submask = (submask - 1) & mask) {  // Total Time: O(m * 3^n)                    if (__builtin_popcount(submask) > k) {                        continue;                    }                    update(d, r, s, mask, submask);                }            } else {                for (int i = 0; i < n; ++i) {  // Total Time: O(n * m * 2^n)                    if (mask & (1 << i)) {                        continue;                    }                    update(d, r, s, mask, 1 << i);                }            }        }        return -1.0;    }}; 

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 ↗