Hunter01

给定一个用字符数组表示的 CPU 需要执行的任务列表。其中包含使用大写的 A - Z 字母表示的26 种不同种类的任务。任务可以以任意顺序执行,并且每个任务都可以在 1 个单位时间内执行完。CPU 在任何一个单位时间内都可以执行一个任务,或者在待命状态。

然而,两个相同种类的任务之间必须有长度为 n 的冷却时间,因此至少有连续 n 个单位时间内 CPU 在执行不同的任务,或者在待命状态。

你需要计算完成所有任务所需要的最短时间。

https://leetcode-cn.com/problems/task-scheduler/solution/tong-si-xiang-jian-ji-gao-xiao-by-hzhu212/

class Solution {
public:
	int leastInterval(vector<char>& tasks, int n) {
		int len = tasks.size();
		vector<int> vis(26);
		for (auto c : tasks) vis[c - \'A\']++;
		sort(vis.begin(), vis.end(), [](int& x, int&y) {return x > y; });
		int count = 1;
		while (count<vis.size()&&vis[count]==vis[0]){
			++count;
		}
		return max(len, (vis[0] - 1)*(n + 1) + count);
	}
};

分类:

技术点:

相关文章:

  • 2022-02-05
  • 2022-01-16
  • 2021-06-11
  • 2021-09-26
  • 2021-04-18
  • 2021-04-05
猜你喜欢
  • 2022-02-13
  • 2022-03-06
  • 2022-02-05
  • 2021-06-07
  • 2021-05-22
  • 2022-02-12
  • 2022-01-01
相关资源
相似解决方案