首页 > 其他分享 >LeetCode 1834. Single-Threaded CPU

LeetCode 1834. Single-Threaded CPU

时间:2024-08-12 11:26:52浏览次数:12  
标签:Available task tasks int LeetCode Single Threaded time CPU

原题链接在这里:https://leetcode.com/problems/single-threaded-cpu/description/

题目:

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

题解:

First, we need to construct a new array with [taskInd, startTime, processingTime].

Sort the array with startTime.

Have a minHeap on first processingTime, then taskInd.

While result index < n, n = task.length, first add all the task that startTime <= current time into the minHeap.

If minHeap is not empty, we can poll the first value add taskInd to the result and udpate the currentTime.

Otherwise, update the currentTime to the current task startTime.

Time Complexity: O(nlogn).

Space: O(n).

AC Java:

 1 class Solution {
 2     public int[] getOrder(int[][] tasks) {
 3         int n = tasks.length;
 4         int[][] sortedTask = new int[n][3];
 5         for(int i = 0; i < n; i++){
 6             sortedTask[i] = new int[]{i, tasks[i][0], tasks[i][1]};
 7         }
 8 
 9         Arrays.sort(sortedTask, (a, b) -> a[1] - b[1]);
10         PriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b) -> a[2] == b[2] ? a[0] - b[0] : a[2] - b[2]);
11 
12         int time = 0;
13         int[] res = new int[n];
14         int ind = 0;
15         int taskInd = 0;
16         while(ind < n){
17             while(taskInd < n && sortedTask[taskInd][1] <= time){
18                 minHeap.add(sortedTask[taskInd]);
19                 taskInd++;
20             }
21 
22             if(!minHeap.isEmpty()){
23                 int[] cur = minHeap.poll();
24                 res[ind++] = cur[0];
25                 time += cur[2];
26             }else{
27                 time = sortedTask[taskInd][1];
28             }
29         }
30 
31         return res;
32     }
33 }

 

标签:Available,task,tasks,int,LeetCode,Single,Threaded,time,CPU
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18354632

相关文章

  • 设计模式 - Singleton pattern 单例模式
    文章目录定义单例模式的实现构成构成UML图单例模式的六种实现懒汉式-线程不安全懒汉式-线程安全饿汉式-线程安全双重校验锁-线程安全静态内部类实现枚举实现总结其他设计模式文章:最后定义单例模式是一种创建型设计模式,它用来保证一个类只有一个实例,并且提供一个......
  • LeetCode 22. 括号生成 回溯写法详解
    22.括号生成22.括号生成题目来源题目分析题目难度题目标签题目限制解题思路核心算法步骤代码实现代码解读性能分析测试用例扩展讨论优化写法其他实现总结22.括号生成题目来源22.括号生成题目分析给定一个数字n,表示生成括号的对数,要求设计一个函数生成所......
  • LeetCode 216. 组合总和 III 回溯写法详解
    216.组合总和III216.组合总和III题目来源题目分析题目难度题目标签题目限制解题思路核心算法步骤代码实现代码解读性能分析测试用例扩展讨论优化写法其他实现总结216.组合总和III题目来源216.组合总和III题目分析题目要求找出所有相加之和为n的k......
  • Leetcode 热题100 - 155 最小栈
    Leetcode热题100-155最小栈1.题目描述2.解题思路3.代码实现(方法二)4.c++知识点用法1.题目描述155最小栈2.解题思路方法一:创建一个辅助栈min_stk用以存储当前元素相对应的栈中最小元素值;方式二:类似于方法一,使用pair<int,int>同时存储当前元素与其对应......
  • Leetcode-3129 找出所有稳定的二进制数组I
    Leetcode-3129找出所有稳定的二进制数组I1.题目描述2.解题思路3.代码实现1.题目描述3129找出所有稳定的二进制数组I2.解题思路(1)定义f[i][j][k]表示i个0、j个1且当前位i+j填写值为k=0/1的所有情况;(2)对于f[i][0][0]、f[0][j][1]初始化为1,注意到:......
  • Leetcode-3132 找出与数组相加的整数II
    Leetcode-3132找出与数组相加的整数II1.题目描述2.解题思路3.代码实现1.题目描述3132找出与数组相加的整数II2.解题思路(1)排序后,注意到nums1数组比nums2数组多两个元素,可推出最小匹配元素一定在nums[0]、nums[1]、nums[2]中出现;(2)优先从nums[2]进行判......
  • 「LeetCode Top100」之滑动窗口
    3.无重复字符的最长子串题目链接:https://leetcode.cn/problems/longest-substring-without-repeating-characters/description/?envType=study-plan-v2&envId=top-100-liked题目难度:中等标签:哈希表、字符串、滑动窗口题目状态:学习题解思路:滑动窗口的思路,也就是维持一个无......
  • LeetCode 算法:最小栈 c++
    原题链接......
  • 「LeetCode Top100」之双指针
    283.移动零题目链接:https://leetcode.cn/problems/move-zeroes/description/?envType=study-plan-v2&envId=top-100-liked题目难度:简单标签:数组、双指针题目状态:AC思路:两个指针,i用来找0,j用来找非0。当nums[i]==0&&nums[j]!=0时,将两者交换。代码:classSolutio......
  • Leetcode 206. 反转链表
    给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。示例1:输入:head=[1,2,3,4,5]输出:[5,4,3,2,1]示例2:输入:head=[1,2]输出:[2,1]示例3:输入:head=[]输出:[]提示: 链表中节点的数目范围是 [0,5000]-5000<=Node.val<=5000方法一: //双指......