-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask-scheduler.cpp
More file actions
32 lines (32 loc) · 876 Bytes
/
Copy pathtask-scheduler.cpp
File metadata and controls
32 lines (32 loc) · 876 Bytes
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
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
int m = tasks.size();
if(m == 0) return 0;
if(m == 1) return 1;
int freq[26] = {0}, next[26];
memset(next , 0, sizeof(next));
for(int i = 0; i < m; i++) {
freq[tasks[i] -'A']++;
}
int time = 0, curTask = 0, count = 0;
int rem = m;
while(rem > 0) {
if(freq[curTask] == 0) {
curTask = (curTask+1)%26;
continue;
}
if(next[curTask] > time) {
count += next[curTask] - time;
time = next[curTask];
}
count++;
time++;
next[curTask] += n+1;
freq[curTask]--;
curTask = (curTask+1)%26;
rem--;
}
return count;
}
};