LC.P621[任务调度器]

方法一:桶原理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int leastInterval(char[] tasks, int n) {
int[] cnt = new int[26];
int max = 0, total = 0;
for (char task : tasks) ++cnt[task - 'A'];
for (int i = 0; i < 26; ++i) {
max = Math.max(max, cnt[i]);
}
for (int i = 0; i < 26; ++i) {
total += max == cnt[i] ? 1 : 0;
}
return Math.max(tasks.length, (n + 1) * (max - 1) + total);
}
}
  • 时间复杂度:$O(n + C)$, $C = 26$
  • 空间复杂度:$O(C)$