-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay 125: Task Scheduler.cpp
More file actions
35 lines (25 loc) · 892 Bytes
/
Copy pathDay 125: Task Scheduler.cpp
File metadata and controls
35 lines (25 loc) · 892 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
33
34
35
int leastInterval(int N, int k, vector<char> &tasks) {
// code here
int counter[26] = {0};
int maxF = 0; // max_freq
int maxFc = 0; // no of ch having max_freq
for (int i = 0; i < N; i++) {
counter[tasks[i] - 'A']++;
}
for (int x : counter) {
if (maxF == x) {
maxFc++;
}
if (maxF < x) {
maxF = x;
maxFc = 1;
}
}
int gaps = maxF - 1; // number of gaps required
int gaps_len = k - (maxFc - 1); // number of ch that can be fit in partition gaps
int avail_slot = gaps * gaps_len; // empty slots = number of gaps * gaps_len
int avail_task = N - maxF * maxFc;
int idles = max(0, avail_slot - avail_task); // place available tasks in total available
// slots and rest as idle
return N + idles;
}