Skip to content

Commit 1f6b101

Browse files
committed
Runtime: 177 ms (Top 67.53%) | Memory: 102.50 MB (Top 17.88%)
1 parent 272cad3 commit 1f6b101

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
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+
23
class Solution {
34
public:
45
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;
1031
}
11-
return curday;
32+
33+
return currentDay;
1234
}
13-
};
35+
};
36+

0 commit comments

Comments
 (0)