20240515 2589 - Hard - Pointer Tree - multi-dimensional array
2589. Minimum Time to Complete All Tasks
暴力
1 <= tasks.length <= 2000
1 <= starti, endi <= 2000
由于题目要求的数值空间范围并不大,我们可以考虑暴力法,用一个数组标记 activate (run) 的时间
- 按结束时间升序排序。
- 从前往后遍历数组,对于每个 task,看其所需的 duration 是否已经被已有的 activate 的时间满足。
- 如果没有被满足,则需要新增 active 的时间点,从后往前遍历
range(end, start - 1, -1)
找到可以 activate 的时间点。这样子可以尽可能的使 active 的时间点被后面的 task 使用。
class Solution:
def findMinimumTime(self, tasks: List[List[int]]) -> int:
tasks = sorted(tasks, key=lambda task: task[1])
activeTime = [0] * (tasks[-1][1] + 1)
for start, end, d in tasks:
activeMins = sum(activeTime[start : end + 1])
needMins = d - activeMins
if needMins > 0:
for i in range(end, start - 1, -1):
if activeTime[i] == 0:
activeTime[i] = 1
needMins -= 1
if needMins == 0:
break
return sum(activeTime)
