-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_2365_TaskSchedulerII.cpp
67 lines (62 loc) · 1.99 KB
/
LC_2365_TaskSchedulerII.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
https://leetcode.com/problems/task-scheduler-ii/
2365. Task Scheduler II
*/
class Solution {
public:
// long long taskSchedulerII(vector<int>& tasks, int space) {
// long long days=0;
// unordered_map<long long,long long> um; //stores the last day the task was executed
// for(int t: tasks)
// {
// days++;
// // cout<<days<<" ";
// if(um.find(t) == um.end())
// um[t] = days;
// else
// {
// long long oldday = um[t];
// if(oldday + space + 1 <= days) {
// um[t] = days;
// }
// else {
// um[t] = days = oldday + space +1;
// }
// }
// // cout<<t<<" "<<um[t]<<endl;
// }
// return days;
// }
long long taskSchedulerII_(vector<int>& tasks, int space) {
long long days=0;
unordered_map<int,long long> hash; //hashmap to store previous day on which task occured
for(int t: tasks)
{
// days++;
// if(hash.count(t) and days - hash[t] <= space)
// days += space - (days-hash[t]) + 1;
// hash[t] = days;
if(hash.count(t))
hash[t] = days = max(days, hash[t] + space)+1;
else
hash[t] = days = days+1;
// cout<<days<<" ";
// cout<<t<<" "<<hash[t]<<endl;
}
return days;
}
long long taskSchedulerII(vector<int>& tasks, int space) {
long long days=0;
unordered_map<int,long long> next; //hashmap to store previous day on which task occured
for(int t: tasks)
{
if(next.count(t))
days = max(days, next[t]);
next[t] = days+space+1;
days+=1;
// cout<<days<<"= ";
// cout<<t<<"-> "<<next[t]<<endl;
}
return days;
}
};