-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00621-task_scheduler.cpp
61 lines (45 loc) · 1.17 KB
/
00621-task_scheduler.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
// 621: Task Scheduler
// https://leetcode.com/problems/task-scheduler
#include <vector>
#include <iostream>
#include <unordered_map>
#include <queue>
using namespace std;
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
unordered_map<char, int> counts;
for (char t : tasks) counts[t]++;
priority_queue<int> pq;
for (pair<char, int> count : counts)
pq.push(count.second);
int result = 0;
int cycle = n + 1;
while (!pq.empty()) {
int time = 0;
vector<int> t;
for (int i = 0; i < cycle; i++) {
if (!pq.empty()) {
t.push_back(pq.top());
pq.pop();
time++;
}
}
for (int cnt : t)
if (--cnt)
pq.push(cnt);
result += !pq.empty() ? cycle : time;
}
return result;
}
};
int main() {
Solution o;
// INPUT
vector<char> tasks = {'A','C','A','B','D','B'};
int n = 1;
// OUTPUT
auto result = o.leastInterval(tasks, n);
cout << result << endl;
return 0;
}