描述
(搬运)
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.
Example 1:
1 | Input: blocks = [1], split = 1 |
Example 2:
1 | Input: blocks = [1,2], split = 5 |
Example 3:
1 | Input: blocks = [1,2,3], split = 1 |
Constraints:
1 | 1 <= blocks.length <= 1000 |
思路
- 二分模板题. 枚举时间限制, 用一个
check/can函数判断任务是否在这种限制内可以完成. - 利用
haffman编码的思路. 贪心原则是用一个(分裂)出现更早的工人, 去完成代价更大的任务, 这样永远比反过来所需时间要少.
代码
二分:
1 | class Solution(object): |
贪心:
1 | class Solution(object): |