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。

相关推荐
_Itachi__3 小时前
LeetCode 热题 100 74. 搜索二维矩阵
算法·leetcode·矩阵
chao_7895 小时前
链表题解——两两交换链表中的节点【LeetCode】
数据结构·python·leetcode·链表
编程绿豆侠7 小时前
力扣HOT100之多维动态规划:1143. 最长公共子序列
算法·leetcode·动态规划
dying_man15 小时前
LeetCode--24.两两交换链表中的结点
算法·leetcode
yours_Gabriel15 小时前
【力扣】2434.使用机器人打印字典序最小的字符串
算法·leetcode·贪心算法
GGBondlctrl17 小时前
【leetcode】递归,回溯思想 + 巧妙解法-解决“N皇后”,以及“解数独”题目
算法·leetcode·n皇后·有效的数独·解数独·映射思想·数学思想
枫景Maple1 天前
LeetCode 2297. 跳跃游戏 VIII(中等)
算法·leetcode