There is a computer that can run an unlimited number of tasks at the same time. You are given a 2D integer array tasks where tasks[i] = [starti, endi, durationi] indicates that the ith task should run for a total of
durationi seconds (not necessarily continuous) within the inclusive time range [starti, endi].
You may turn on the computer only when it needs to run a task. You can also turn it off if it is idle.
Return the minimum time during which the computer should be turned on to complete all tasks.
Input: tasks =[[2,3,1],[4,5,1],[1,5,2]]Output: 2Explanation:
- The first task can be run in the inclusive time range [2,2].- The second task can be run in the inclusive time range [5,5].- The third task can be run in the two inclusive time ranges [2,2] and [5,5].The computer will be on for a total of 2 seconds.
Input: tasks =[[1,3,2],[2,5,3],[5,6,2]]Output: 4Explanation:
- The first task can be run in the inclusive time range [2,3].- The second task can be run in the inclusive time ranges [2,3] and [5,5].- The third task can be run in the two inclusive time range [5,6].The computer will be on for a total of 4 seconds.
To minimize the computer’s on-time, we schedule tasks as late as possible within their allowed intervals. By processing tasks in order of their end times, we can greedily fill the latest available slots, ensuring overlap and minimizing total on-time.
classSolution {
publicintfindMinimumTime(int[][] tasks) {
Arrays.sort(tasks, Comparator.comparingInt(a -> a[1]));
boolean[] on =newboolean[2001];
int ans = 0;
for (int[] t : tasks) {
int cnt = 0;
for (int i = t[0]; i <= t[1]; i++) if (on[i]) cnt++;
int need = t[2]- cnt;
for (int i = t[1]; need > 0 && i >= t[0]; i--) {
if (!on[i]) { on[i]=true; ans++; need--; }
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funfindMinimumTime(tasks: Array<IntArray>): Int {
tasks.sortBy { it[1] }
val on = BooleanArray(2001)
var ans = 0for (t in tasks) {
var cnt = 0for (i in t[0]..t[1]) if (on[i]) cnt++var need = t[2] - cnt
for (i in t[1] downTo t[0]) {
if (need ==0) breakif (!on[i]) { on[i] = true; ans++; need-- }
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
deffindMinimumTime(self, tasks: list[list[int]]) -> int:
tasks.sort(key=lambda x: x[1])
on = [False] *2001 ans =0for s, e, d in tasks:
cnt = sum(on[s:e+1])
need = d - cnt
for i in range(e, s-1, -1):
if need ==0: breakifnot on[i]: on[i] =True; ans +=1; need -=1return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
impl Solution {
pubfnfind_minimum_time(tasks: Vec<Vec<i32>>) -> i32 {
letmut tasks = tasks;
tasks.sort_by_key(|x| x[1]);
letmut on =vec![false; 2001];
letmut ans =0;
for t in tasks.iter() {
let cnt = (t[0]..=t[1]).filter(|&i| on[i asusize]).count() asi32;
letmut need = t[2] - cnt;
for i in (t[0]..=t[1]).rev() {
if need ==0 { break; }
if!on[i asusize] { on[i asusize] =true; ans +=1; need -=1; }
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
findMinimumTime(tasks: number[][]):number {
tasks.sort((a, b) =>a[1] -b[1]);
conston= Array(2001).fill(false);
letans=0;
for (const [s, e, d] oftasks) {
letcnt=0;
for (leti=s; i<=e; i++) if (on[i]) cnt++;
letneed=d-cnt;
for (leti=e; need>0&&i>=s; i--) {
if (!on[i]) { on[i] =true; ans++; need--; }
}
}
returnans;
}
}