File tree 1 file changed +34
-1
lines changed
1 file changed +34
-1
lines changed Original file line number Diff line number Diff line change 8
8
Time: O(n)
9
9
Space: O(1)
10
10
*/
11
-
11
+ /*
12
12
class Solution {
13
13
public:
14
14
int leastInterval(vector<char>& tasks, int n) {
@@ -38,3 +38,36 @@ class Solution {
38
38
return tasks.size() + idles;
39
39
}
40
40
};
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
+ };
You can’t perform that action at this time.
0 commit comments