# 飞书很好，但赢不了，只能裁员

10,156 阅读5分钟

## 心碎飞书

3 月 26 日，字节跳动旗下产品飞书的 CEO 谢欣发布全员信，正式宣布进行新一轮的组织调整，即裁员。

...

## 题目描述

输入：tasks = ["A","A","A","B","B","B"], n = 2

在本示例中，两个相同类型任务之间必须间隔长度为 n = 2 的冷却时间，而执行一个任务只需要一个单位时间，所以中间出现了（待命）状态。


输入：tasks = ["A","A","A","B","B","B"], n = 0

["A","A","A","B","B","B"]
["A","B","A","B","A","B"]
["B","B","B","A","A","A"]
...



输入：tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2

A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> (待命) -> (待命) -> A -> (待命) -> (待命) -> A


• $1 <= task.length <= 10^4$
• tasks[i] 是大写英文字母
• n 的取值范围为 $[0, 100]$

## 构造

$\max(task.length, (n + 1) \times (max - 1) + tot)$

Java 代码：

class Solution {
public int leastInterval(char[] tasks, int n) {
int[] cnts = new int[26];
for (char c : tasks) cnts[c - 'A']++;
int max = 0, tot = 0;
for (int i = 0; i < 26; i++) max = Math.max(max, cnts[i]);
for (int i = 0; i < 26; i++) tot += max == cnts[i] ? 1 : 0;
return Math.max(tasks.length, (n + 1) * (max - 1) + tot);
}
}


C++ 代码：

class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
vector<int> cnts(26, 0);
for (char c : tasks) cnts[c - 'A']++;
int maxv = *max_element(cnts.begin(), cnts.end());
int tot = count(cnts.begin(), cnts.end(), maxv);
return max(static_cast<int>(tasks.size()), (n + 1) * (maxv - 1) + tot);
}
};


Python 代码：

class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
cnts = [0] * 26
cnts[ord(c) - ord('A')] += 1
maxv, tot = 0, 0
for i in range(26):
maxv = max(maxv, cnts[i])
for i in range(26):
tot += 1 if maxv == cnts[i] else 0
return max(len(tasks), (n + 1) * (maxv - 1) + tot)


TypeScript 代码：

function leastInterval(tasks: string[], n: number): number {
const cnts = new Array<number>(26).fill(0)
for (const c of tasks) cnts[c.charCodeAt(0) - 'A'.charCodeAt(0)]++
let max = 0, tot = 0
for (let i = 0; i < 26; i++) max = Math.max(max, cnts[i])
for (let i = 0; i < 26; i++) tot += max == cnts[i] ? 1 : 0
return Math.max(tasks.length, (n + 1) * (max - 1) + tot)
}

• 时间复杂度：$O(n + C)$
• 空间复杂度：$O(C)$，其中 $C = 26$ 为任务字符集大小

## 最后

📅 年度会员：有效期加赠两个月！！; 季度会员：有效期加赠两周！！

🧧 年度会员：获 66.66 现金红包！！; 季度会员：获 22.22 现金红包！！

🎁 年度会员：参与当月丰厚专属实物抽奖（中奖率 > 30%)！！