Leetcode 2895. Minimum Processing Time

  • [Leetcode 2895. Minimum Processing Time](#Leetcode 2895. Minimum Processing Time)
    • [1. 解题思路](#1. 解题思路)
    • [2. 代码实现](#2. 代码实现)

1. 解题思路

这一题整体上来说其实没啥难度,就是一个greedy算法,只需要想明白耗时长的任务一定要优先执行,不存在某个耗时长的任务后执行可以更快的完成的情况。

因此,我们只需要将耗时倒序排列之后顺序分配给各个闲置的CPU即可。

2. 代码实现

给出python代码实现如下:

python 复制代码
class Solution:
    def minProcessingTime(self, processorTime: List[int], tasks: List[int]) -> int:
        processorTime = sorted(processorTime)
        tasks = sorted(tasks, reverse=True)
        n = len(processorTime)
        ans = max(processorTime[i] + tasks[4*i] for i in range(n))
        return ans

提交代码评测得到:耗时591ms,占用内存32.8MB。

相关推荐
失去的青春---夕阳下的奔跑10 小时前
560. 和为 K 的子数组
数据结构·算法·leetcode
m0_6294947311 小时前
LeetCode 热题 100-----25.回文链表
数据结构·算法·leetcode·链表
吃着火锅x唱着歌13 小时前
LeetCode 1019.链表中的下一个更大节点
算法·leetcode·链表
凌波粒14 小时前
LeetCode--404.左叶子之和(二叉树)
算法·leetcode·职场和发展
绝知此事14 小时前
【算法突围 03】核心算法思想:分治/递归/动态规划与 LeetCode 高频真题解析
算法·leetcode·面试·动态规划
阿Y加油吧16 小时前
两道字符串 DP 模板题复盘:最长公共子序列 & 编辑距离
leetcode
我爱cope16 小时前
【力扣hot100:76. 最小覆盖子串】
算法·leetcode·职场和发展
sheeta199816 小时前
LeetCode 每日一题笔记 日期:2026.05.20 题目:2657. 找到前缀公共数组
笔记·算法·leetcode
吃着火锅x唱着歌17 小时前
LeetCode 962.最大宽度坡
算法·leetcode·职场和发展
凌波粒17 小时前
LeetCode--257. 二叉树的所有路径(二叉树)
算法·leetcode·职场和发展