1
class Solution {
2
public:
3
long long taskSchedulerII(vector<int> &tasks, int space) {
4
// Use a map to keep track of the last occurrence day for each task
5
unordered_map<int, long long> lastOccurrence;
6

7
// Initialize the current day to 0
8
long long currentDay = 0;
9

10
// Loop through each task in the list
11
for (int task : tasks) {
12
// If the task has been executed before
13
if (lastOccurrence.find(task) != lastOccurrence.end()) {
14
// Check if the space constraint is satisfied
15
if (currentDay - lastOccurrence[task] <= space) {
16
// If not, move the currentDay ahead
17
currentDay = lastOccurrence[task] + space + 1;
18
} else {
19
// If yes, just increment the current day
20
currentDay++;
21
}
22
} else {
23
// If the task hasn't been executed before, simply increment the current
24
// day
25
currentDay++;
26
}
27

28
// Update the last occurrence of the task
29
lastOccurrence[task] = currentDay;
30
}
31

32
return currentDay;
33
}
34
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0