-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_621_TaskScheduler.cpp
49 lines (32 loc) · 1.14 KB
/
LC_621_TaskScheduler.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
/*
https://leetcode.com/problems/task-scheduler/
621. Task Scheduler
*/
class Solution {
public:
int leastInterval_1(vector<char>& tasks, int d) {
if(d==0)
return tasks.size();
int freq[26] = {0};
for(char &t: tasks)
freq[t-'A']++;
const int maxFreq = *max_element(freq, freq+26);
const int numMaxFreqEle = count(freq, freq+26, maxFreq);
//slots of d+1 size,
const int maxFreqTaskOccupy = (maxFreq-1)*(d+1);
return max(static_cast<int>(tasks.size()), maxFreqTaskOccupy + numMaxFreqEle);
}//end
int leastInterval(vector<char>& tasks, int d) {
if(d==0)
return tasks.size();
int freq[26] = {0};
for(char &t: tasks)
freq[t-'A']++;
sort(freq, freq+26);
int max_val = freq[25]-1;
int idle_slots = max_val * d;
for(int i=24; i>=0; i--)
idle_slots -= min(freq[i], max_val);
return idle_slots>0 ? idle_slots+tasks.size(): tasks.size();
}//end
};