当前位置: 首页 > news >正文

Leetcode 1834. Single-Threaded CPU (堆好题)

  1. Single-Threaded CPU
    Medium

You are given n​​​​​​ tasks labeled from 0 to n - 1 represented by a 2D integer array tasks, where tasks[i] = [enqueueTimei, processingTimei] means that the i​​​​​​th​​​​ task will be available to process at enqueueTimei and will take processingTimei to finish processing.

You have a single-threaded CPU that can process at most one task at a time and will act in the following way:

If the CPU is idle and there are no available tasks to process, the CPU remains idle.
If the CPU is idle and there are available tasks, the CPU will choose the one with the shortest processing time. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.
Once a task is started, the CPU will process the entire task without stopping.
The CPU can finish a task then start a new one instantly.
Return the order in which the CPU will process the tasks.

Example 1:

Input: tasks = [[1,2],[2,4],[3,2],[4,1]]
Output: [0,2,3,1]
Explanation: The events go as follows:

  • At time = 1, task 0 is available to process. Available tasks = {0}.
  • Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}.
  • At time = 2, task 1 is available to process. Available tasks = {1}.
  • At time = 3, task 2 is available to process. Available tasks = {1, 2}.
  • Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}.
  • At time = 4, task 3 is available to process. Available tasks = {1, 3}.
  • At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}.
  • At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}.
  • At time = 10, the CPU finishes task 1 and becomes idle.
    Example 2:

Input: tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]
Output: [4,3,2,0,1]
Explanation: The events go as follows:

  • At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}.
  • Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}.
  • At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}.
  • At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}.
  • At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}.
  • At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}.
  • At time = 40, the CPU finishes task 1 and becomes idle.

Constraints:

tasks.length == n
1 <= n <= 105
1 <= enqueueTimei, processingTimei <= 109

解法1:
这题感觉不容易。我一开始想的是把3个变量(enqueueTime, procTime, index)放到一个Node节点里面,然后用minHeap来做。
后来发现不好处理,因为每次CPU处理一个任务完后,会有一些新的curTime >= enqueueTime的任务变得可行,这个只用minHeap来做是不行的,因为我们不能一个个pop出来检查,再把可以的放回去。
参考的网上的做法。用pair<processTime, index>来作为minHeap的Node,用pair<int, pair<int, int>>> // <enQueueTime, pair<processTime, index>>来构成一个数组nodes,并排序。每次我们从minHeap里面取出top来处理后,调整curTime,再把数组nodes里面的可行的任务push到minHeap里面去。注意每次从minHeap里面只能取一个任务,不能用while,因为每个任务处理完以后,又有一些新的任务可行,这些新的任务可能比当前的top还应该先处理。

long long curTime = 0;class Solution {
public:vector<int> getOrder(vector<vector<int>>& tasks) {int n = tasks.size();//priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> minHeap; //<processTime, index>priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minHeap; //<processTime, index>vector<pair<int, pair<int, int>>> nodes; // <enQueueTime, pair<processTime, index>>vector<int> res;for (int i = 0; i < n; i++) {nodes.push_back({tasks[i][0], {tasks[i][1], i}});}sort(nodes.begin(), nodes.end());int index = 0;while (res.size() < n) {if (!minHeap.empty()) { //注意这里不能用while,因为每个任务处理完以后,又有一些新的任务可行,这些新的任务可能比当前的top还应该先处理。auto topNode = minHeap.top();minHeap.pop();res.push_back(topNode.second);curTime += topNode.first;} else if (index < n) {curTime = nodes[index].first;} else break;for (; index < n; index++) {if (curTime >= nodes[index].first) {minHeap.push(nodes[index].second);} else break;}}return res;}
};
http://www.lryc.cn/news/196152.html

相关文章:

  • 21-数据结构-内部排序-交换排序
  • 5-k8s-探针介绍
  • 【网络安全 --- MySQL数据库】网络安全MySQL数据库应该掌握的知识,还不收藏开始学习。
  • 【MyBatis系列】- 什么是MyBatis
  • 【Linux】Ubuntu美化bash【教程】
  • 微信小程序仿苹果负一屏由弱到强的高斯模糊
  • js中的new方法
  • 机器学习-无监督算法之降维
  • ubuntu20.04下Kafka安装部署及基础使用
  • 汉得欧洲x甄知科技 | 携手共拓全球化布局,助力出海中企数智化发展
  • 【Javascript保姆级教程】显示类型转换和隐式类型转换
  • C++算法前缀和的应用:分割数组的最大值的原理、源码及测试用例
  • gitlab自编译 源码下载
  • SBD(Schottky Barrier Diode)与JBS(Junction Barrier Schottky)
  • HANA:计算视图-图形化Aggregation组件-踩坑小记(注意事项)
  • 【milkv】更新rndis驱动
  • 基于混沌博弈优化的BP神经网络(分类应用) - 附代码
  • 基于人工水母优化的BP神经网络(分类应用) - 附代码
  • 【C++】哈希学习
  • Nginx的安装——window环境
  • C语言笔记之指针
  • 【 OpenGauss源码学习 —— 列存储(CU)(二)】
  • Java并发面试题:(四)synchronized和lock区别
  • 使用Nginx实现采集端和数据分析平台的数据加密传输
  • appium---如何判断原生页面和H5页面
  • 【WIFI】【WPS】如何从log角度判断WPS 已经连接上
  • [正式学习java①]——java项目结构,定义类和创建对象,一个标准javabean的书写
  • day36
  • 五. 激光雷达建图和定位方案-开源SLAM
  • SAP MM学习笔记37 - 请求书照合中的 追加请求/追加Credit 等概念/ 请求书的取消