1
- // Runtime: 380 ms (Top 65.56%) | Memory: 102 MB (Top 63.00%)
1
+ // Runtime: 177 ms (Top 67.53%) | Memory: 102.50 MB (Top 17.88%)
2
+
2
3
class Solution {
3
4
public:
4
5
long long taskSchedulerII (vector<int >& tasks, int space) {
5
- unordered_map<int ,long long int > hash; long long int curday=0 ;
6
- for (int i=0 ;i<tasks.size ();i++){
7
- if (hash.count (tasks[i])) curday=max (curday,hash[tasks[i]]);
8
- hash[tasks[i]]=curday+space+1 ;
9
- curday+=1 ;
6
+ // Use a map to keep track of the last occurrence day for each task
7
+ unordered_map<int , long long > lastOccurrence;
8
+
9
+ // Initialize the current day to 0
10
+ long long currentDay = 0 ;
11
+
12
+ // Loop through each task in the list
13
+ for (int task : tasks) {
14
+ // If the task has been executed before
15
+ if (lastOccurrence.find (task) != lastOccurrence.end ()) {
16
+ // Check if the space constraint is satisfied
17
+ if (currentDay - lastOccurrence[task] <= space) {
18
+ // If not, move the currentDay ahead
19
+ currentDay = lastOccurrence[task] + space + 1 ;
20
+ } else {
21
+ // If yes, just increment the current day
22
+ currentDay++;
23
+ }
24
+ } else {
25
+ // If the task hasn't been executed before, simply increment the current day
26
+ currentDay++;
27
+ }
28
+
29
+ // Update the last occurrence of the task
30
+ lastOccurrence[task] = currentDay;
10
31
}
11
- return curday;
32
+
33
+ return currentDay;
12
34
}
13
- };
35
+ };
36
+
0 commit comments