class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
vector<int> v(26);
int freq = 0;
for (auto c : tasks)
freq = max(++v[c - 'A'], freq);
int count = 0;
for (int i = 0; i < v.size(); ++i)
if (v[i] == freq) count++;
int with_idle = (n + 1) * (freq - 1) + count;
int no_idle = tasks.size();
return max(with_idle, no_idle);
}
};
max frequency tasks를 제외한 tasks에 대해,
- (n + 1) 슬롯에서의 idle time을 채우거나 남는다면 : tasks.size
- idle time을 채우지 못한다면 : (n + 1) * (freq - 1) + count
(priority_queue를 활용한 greedy 방식이 일반적인 풀이)
'알고리즘 > leetcode' 카테고리의 다른 글
417. Pacific Atlantic Water Flow (0) | 2022.08.26 |
---|---|
173. Binary Search Tree Iterator (0) | 2022.08.24 |
437. Path Sum III (0) | 2022.08.14 |
1706. Where Will the Ball Fall (0) | 2022.07.31 |
234. Palindrome Linked List (0) | 2022.07.30 |