File tree Expand file tree Collapse file tree 2 files changed +65
-29
lines changed
Expand file tree Collapse file tree 2 files changed +65
-29
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ class Solution:
3+ def leastInterval(self, tasks: List[str], n: int) -> int:
4+ td = dict()
5+ count_most_frequent = 0
6+ for task in tasks:
7+ if task not in td:
8+ td[task] = 1
9+ else:
10+ td[task] += 1
11+ count_most_frequent = max(count_most_frequent, td[task])
12+
13+ result = (count_most_frequent - 1) * (n + 1)
14+
15+ for task in td.keys():
16+ if count_most_frequent == td[task]:
17+ result += 1
18+ return max(result, len(tasks))
19+ */
20+
21+
22+ class Solution {
23+ /*
24+ Based on solution - https://leetcode.com/problems/task-scheduler/discuss/104493/C%2B%2B-Java-Clean-Code-Priority-Queue
25+ */
26+ public:
27+ int leastInterval (vector<char >& tasks, int n) {
28+ unordered_map<char , int > counts;
29+ for (char t : tasks) {
30+ counts[t]++;
31+ }
32+ priority_queue<int > pq;
33+ for (pair<char , int > count : counts) {
34+ pq.push (count.second );
35+ }
36+ int alltime = 0 ;
37+ int cycle = n + 1 ;
38+ while (!pq.empty ()) {
39+ int time = 0 ;
40+ vector<int > tmp;
41+ for (int i = 0 ; i < cycle; i++) {
42+ if (!pq.empty ()) {
43+ tmp.push_back (pq.top ());
44+ pq.pop ();
45+ time++;
46+ }
47+ }
48+ for (int cnt : tmp) {
49+ if (--cnt) {
50+ pq.push (cnt);
51+ }
52+ }
53+ alltime += !pq.empty () ? cycle : time;
54+ }
55+ return alltime;
56+ }
57+ };
58+
59+
60+
61+
62+
63+
64+
65+
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments