[LeetCode] Task Scheduler II

2365. Task Scheduler II

You are given a 0-indexed array of positive integers tasks, representing tasks that need to be completed in order, where tasks[i] represents the type of the ith task.

You are also given a positive integer space, which represents the minimum number of days that must pass after the completion of a task before another task of the same type can be performed.

Each day, until all tasks have been completed, you must either:

  • Complete the next task from tasks, or
  • Take a break.

Return the minimum number of days needed to complete all tasks.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
long long taskSchedulerII(vector<int>& A, int space) {
long long res = 0;
unordered_map<long long, long long> mp;
for(int i = 0; i < A.size(); i++) {
if(mp[A[i]] > res) res = mp[A[i]];
else res++;
mp[A[i]] = res + space + 1;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/07/PS/LeetCode/task-scheduler-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.