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.mzph.cn/news/110237.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何创建前端自定义主题和样式?

聚沙成塔每天进步一点点 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 欢迎来到前端入门之旅&#xff01;感兴趣的可以订阅本专栏哦&#xff01;这个专栏是为那些对Web开发感兴趣、刚刚踏入前端领域的朋友们量身打造的。无论你是完全的新手还是有一些基础的开发…

PG集合查询

1.运算符 1.1 union并集 连接上下语句 union distinct连接并且去重 all不去重 1.2 intersect交集 上下交集 distinct连接并且去重 all不去重 1.3 except除外 上面除了下面 distinc去重 all不去重

GEE案例——指定区域纯净森林提取分析(红和近红外波段)阈值法提取森林面积

本教程主要是利用影像波段的近红外和红波段的指数作为森林区域的筛选,利用大津法进行指定区域的森林夏季的遥感影像的红波段和近红外波段。 简介: 提取森林范围是遥感影像处理中的一项常见任务。以下是可能用到的一些步骤: 1. 数据预处理:首先,需要进行数据预处理,包括…

Jupyter Notebook在指定位置打开

1、在Jupyter Notebook设置文件中修改默认路径 anconda prompt输入&#xff1a; jupyter notebook --generate-config 找到配置文件路径&#xff1a;C:\Users\Lenovo.jupyter 打开文件&#xff0c;修改默认路径&#xff1a; ## The directory to use for notebooks and kernel…

ORACLE 特殊日期时间转换,计算

一&#xff1a;特殊日期处理 如该字段存储日期形式为&#xff1a;2023/4/23 9:00&#xff0c;2023-3-1 12:23。将这样的数据转换成正确的格式&#xff08;yyyy-mm-dd HH24:mi:ss&#xff09;&#xff0c;即为&#xff1a;2023-04-23 09:00:00。这里举例的字段为&#xff1a;JS…

PostgreSQL中实现数学中的组合问题

记得在学《数值分析》这门课程时,其中有排列组合的相关计算。最近没有想到,自己居然在编程上需要了这个问题,需要在数据库的一张表中,找出任意n条记录的组合,判断组合后的图形是什么,也就是组合问题。从网上搜索的相关资料,发现很多是在Java或者python代码实现的。因为数…

深入理解Java IO流: 包括字节流和字符流的用法、文件读写实践

文章目录 &#x1f4d5;我是廖志伟&#xff0c;一名Java开发工程师、Java领域优质创作者、CSDN博客专家、51CTO专家博主、阿里云专家博主、清华大学出版社签约作者、产品软文创造者、技术文章评审老师、问卷调查设计师、个人社区创始人、开源项目贡献者。&#x1f30e;跑过十五…

Protocols/面向协议编程, DependencyInjection/依赖式注入 的使用

1. Protocols 定义实现协议&#xff0c;面向协议编码 1.1 创建面向协议实例 ProtocolsBootcamp.swift import SwiftUI/// 颜色样式协议 protocol ColorThemeProtocol {var primary: Color { get }var secondary: Color { get }var tertiary: Color { get } }struct DefaultCol…

【基于Kmeans、Kmeans++和二分K均值算法的图像分割】数据挖掘实验三

文章目录 I、项目任务要求II、原理描述KMeansKMeans二分K均值评价指标-轮廓系数 III、数据集描述IV、具体实现过程V、结果分析VI、完整代码VII、深度学习与图片分割&#xff08;补充&#xff09;CNN1. 卷积层&#xff08;Convolutional Layer&#xff09;&#xff1a;2. 激活函…

DAQ进行准确的测量,为决策提供更可靠的依据

进行准确的测量&#xff0c;为决策提供更可靠的依据 DAQExpress提供了交互式分析面板&#xff0c;可帮助您轻松配置兼容的测量硬件&#xff0c;以及查看分析测量数据。无需编程即可立即捕获测量数据&#xff0c;或者在DAQExpress编辑器中创建一个基本的LabVIEW VI&#xff0c;…

LeetCode算法栈—有效的括号

目录 有效的括号 用到的数据结构&#xff1a; 位运算、Map 和 Stack Stack 常用的函数&#xff1a; 题解&#xff1a; 代码&#xff1a; 运行结果; 给定一个只包括 (&#xff0c;)&#xff0c;{&#xff0c;}&#xff0c;[&#xff0c;] 的字符串 s &#xff0c;判断字符…

上海亚商投顾:沪指震荡调整 转基因概念股逆势大涨

上海亚商投顾前言&#xff1a;无惧大盘涨跌&#xff0c;解密龙虎榜资金&#xff0c;跟踪一线游资和机构资金动向&#xff0c;识别短期热点和强势个股。 一.市场情绪 沪指昨日低开低走&#xff0c;深成指、创业板指均跌超1%&#xff0c;双双创出年内新低。转基因概念股逆势大涨…

C语言进行实验:通过程序实现线算图取值【支持VC++ 6.0编辑器环境运行】

背景&#xff1a; 一、实验目的和要求 1、能描述数据基本类型及其常量的表示方法&#xff1b; 2、会对变量进行定义及初始化&#xff1b; 3、能使用运算符与表达式对变量赋值&#xff1b; 4、会描述C语句的概念及种类、C语言常用的输入/出方式&#xff1b; 5、会设计顺序…

Java Cache 缓存方案详解及代码-Ehcache

一、Spring缓存概念 Spring从3.1开始定义了 org.springframework.cache.Cache 和 org.springframework.cache.CacheManager 接口来统一不同的缓存技术&#xff1b; 并支持使用 JCache&#xff08;JSR-107&#xff09; 注解简化我们开发。 常用的缓存实现有 RedisCache 、EhCach…

【达梦数据库】mysql函数改写达梦

yearweek 改写为 year(‘2019-07-11’)||WEEK(‘2019-07-11’,1); mysql> select YEARWEEK(2019-07-11,1); -------------------------- | YEARWEEK(2019-07-11,1) | -------------------------- | 201928 | -------------------------- 1 row in set (0…

解决GET请求入参@NotNull验证不生效问题

一、问题 get请求NotNull验证不生效 二、解决方案 两个步骤&#xff1a; 在该方法的controller类上加Validated&#xff1b;在参数面前加NotNull&#xff1b; 三、其他注解 //被注释的元素必须为null Null //被注释的元素不能为null NotNull //被注释的元素必须为true Ass…

[一带一路金砖 2023 CTF]Crypto

题1 题目描述&#xff1a; from Crypto.Util.number import * from flag import flag import gmpy2 assert(len(flag)38) flag bytes_to_long(flag)p getPrime(512) q getPrime(512)e 304 enc pow(flag,e,p*q) print(p) print(q) print(enc) #9794998439882070838464987…

CSS盒子模型的详细解析

03-盒子模型 作用&#xff1a;布局网页&#xff0c;摆放盒子和内容。 盒子模型-组成 内容区域 – width & height 内边距 – padding&#xff08;出现在内容与盒子边缘之间&#xff09; 边框线 – border 外边距 – margin&#xff08;出现在盒子外面&#xff09; d…

赎金信(C++解法)

题目 给你两个字符串&#xff1a;ransomNote 和 magazine &#xff0c;判断 ransomNote 能不能由 magazine 里面的字符构成。 如果可以&#xff0c;返回 true &#xff1b;否则返回 false 。 magazine 中的每个字符只能在 ransomNote 中使用一次。 示例 1&#xff1a; 输入…

【Git】bad signature 0x00000000 index file corrupt. fatal: index file corrupt

问题描述 电脑写代码时蓝屏。重启后 git commit 出错。 error: bad signature 0x00000000 fatal: index file corrupt原因分析 当电脑发生蓝屏或异常关机时&#xff0c;Git 的索引文件可能损坏。 解决方案 删除损坏的索引文件。 rm -Force .git/index回退到上一个可用的版…