博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
621. Task Scheduler
阅读量:4676 次
发布时间:2019-06-09

本文共 2046 字,大约阅读时间需要 6 分钟。

Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.

However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle.

You need to return the least number of intervals the CPU will take to finish all the given tasks.

 

Example:

Input: tasks = ["A","A","A","B","B","B"], n = 2Output: 8Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.

 

Note:

  1. The number of tasks is in the range [1, 10000].
  2. The integer n is in the range [0, 100].
 

Approach #1: C++. 

class Solution {public:    int leastInterval(vector
& tasks, int n) { int size = tasks.size(); if (size == 0) return 0; vector
temp(30, 0); for (int i = 0; i < size; ++i) { int idx = tasks[i] - 'A'; temp[idx]++; } sort(temp.begin(), temp.end(), [](int& a, int& b) { return a > b; }); int f = temp[0]; int t = 1; for (int i = 1; i < 30; ++i) if (temp[i] == f) t++; else break; int ans = (f - 1) * (n + 1) + t; if (ans < size) return size; else return ans; }};

  

Approach #2: C++. [STL]

// Author: Huahua// Runtime: 56 msclass Solution {public:    int leastInterval(vector
& tasks, int n) { vector
count(26, 0); for (const char task : tasks) ++count[task - 'A']; const int max_count = *max_element(count.begin(), count.end()); size_t ans = (max_count - 1) * (n + 1); ans += count_if(count.begin(), count.end(), [max_count](int c){ return c == max_count; }); return max(tasks.size(), ans); }};

  

Analysis:

 

转载于:https://www.cnblogs.com/ruruozhenhao/p/10212627.html

你可能感兴趣的文章
JS中SetTimeOut和SetInterval方法的区别?
查看>>
Protocol Buffers proto语言语法说明
查看>>
Hibernate双向一对一对象关系模型映射
查看>>
正则表达式(下)
查看>>
熟悉常用的HDFS操作
查看>>
Linux自动化运维第十八课
查看>>
web 10
查看>>
jquery--动态篇
查看>>
npm 是干什么的
查看>>
Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备
查看>>
查找路径php.ini文件到底在哪里?
查看>>
传统认知PK网络认知 刚子扯谈烤串认知
查看>>
字节数组java加密与解密
查看>>
矩形运算
查看>>
php 备份mysql数据库(joomla数据库可直接使用,其他数据库稍作修改即可)
查看>>
使用HttpSessionListener接口监听Session的创建和失效
查看>>
Windows Phone XNAでアニメーション - ぐるぐる
查看>>
20181029 T2 寻宝游戏
查看>>
C++变量作用域、生存期、存储类别
查看>>
数据结构期末复习(四)
查看>>