Skip to content

Commit 5247279

Browse files
authored
Match video's code
Problem: 0621-task-scheduler.cpp Language: C++ Submission URL: https://leetcode.com/problems/task-scheduler/submissions/889595683/
1 parent c944fc6 commit 5247279

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

Diff for: cpp/0621-task-scheduler.cpp

+34-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
Time: O(n)
99
Space: O(1)
1010
*/
11-
11+
/*
1212
class Solution {
1313
public:
1414
int leastInterval(vector<char>& tasks, int n) {
@@ -38,3 +38,36 @@ class Solution {
3838
return tasks.size() + idles;
3939
}
4040
};
41+
*/
42+
43+
// Time O(n * cooldown)
44+
class Solution {
45+
public:
46+
int leastInterval(vector<char>& tasks, int n) {
47+
priority_queue<int> pq;
48+
queue<vector<int>> q;
49+
vector<int> counter(26);
50+
51+
for (int i = 0; i < tasks.size(); ++i)
52+
++counter[tasks[i] - 'A'];
53+
for (int i = 0; i < 26; ++i){
54+
if (counter[i])
55+
pq.push(counter[i]);
56+
}
57+
58+
int time = 0;
59+
while (!q.empty() || !pq.empty()){
60+
++time;
61+
if (!pq.empty()){
62+
if (pq.top() - 1)
63+
q.push({pq.top() - 1, time + n});
64+
pq.pop();
65+
}
66+
if (!q.empty() && q.front()[1] == time){
67+
pq.push(q.front()[0]);
68+
q.pop();
69+
}
70+
}
71+
return time;
72+
}
73+
};

0 commit comments

Comments
 (0)