[LeetCode] Minimum Time to Build Blocks

1199. Minimum Time to Build Blocks

You are given a list of blocks, where blocks[i] = t means that the i-th block needs t units of time to be built. A block can only be built by exactly one worker.

A worker can either split into two workers (number of workers increases by one) or build a block then go home. Both decisions cost some time.

The time cost of spliting one worker into two workers is given as an integer split. Note that if two workers split at the same time, they split in parallel so the cost would be split.

Output the minimum time needed to build all blocks.

Initially, there is only one worker.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
int dp[1010][2020];
int helper(vector<int>& A, int pos, int worker, int& split) {
if(pos < 0) return 0;
if(worker > pos) return A[pos];
if(worker == 0) return 987654321;
if(dp[pos][worker] != -1) return dp[pos][worker];
int& res = dp[pos][worker] = split + helper(A, pos, worker * 2, split);
res = min(res, max(A[pos], helper(A, pos - 1, worker - 1, split)));
return res;
}
public:
int minBuildTime(vector<int>& A, int split) {
memset(dp, -1, sizeof dp);
sort(begin(A), end(A));

return helper(A, A.size() - 1, 1, split);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/24/PS/LeetCode/minimum-time-to-build-blocks/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.