-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0621_Task-Scheduler.cpp
More file actions
36 lines (29 loc) · 984 Bytes
/
0621_Task-Scheduler.cpp
File metadata and controls
36 lines (29 loc) · 984 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
36
class Solution {
public:
int leastInterval(vector<char> &tasks, int n) {
// calculate number of idle
int idle = 0;
int total_task = tasks.size();
int max_frequency = 0;
int num_task_is_max_frequency = 0;
vector<int> cnt(26, 0);
for (auto &c : tasks) {
cnt[c - 'A']++;
}
for (int i = 0; i < cnt.size(); ++i) {
if (max_frequency < cnt[i]) {
max_frequency = cnt[i];
num_task_is_max_frequency = 1;
} else if (max_frequency == cnt[i]) {
num_task_is_max_frequency++;
}
}
int gap = max_frequency - 1;
int gap_len = n - (num_task_is_max_frequency - 1);
int available_empty = gap * gap_len;
int demand_task =
total_task - max_frequency * num_task_is_max_frequency;
idle = max(0, available_empty - demand_task);
return total_task + idle;
}
};