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。

相关推荐
dying_man9 分钟前
LeetCode--42.接雨水
算法·leetcode
黑听人1 小时前
【力扣 困难 C】115. 不同的子序列
c语言·leetcode
前端拿破轮3 小时前
🤡🤡🤡面试官:就你这还每天刷leetcode?连四数相加和四数之和都分不清!
算法·leetcode·面试
无聊的小坏坏5 小时前
单调栈通关指南:从力扣 84 到力扣 42
c++·算法·leetcode
qq_5139704421 小时前
力扣 hot100 Day37
算法·leetcode
不見星空21 小时前
leetcode 每日一题 1865. 找出和为指定值的下标对
算法·leetcode
chao_7891 天前
回溯题解——子集【LeetCode】二进制枚举法
开发语言·数据结构·python·算法·leetcode
GEEK零零七1 天前
Leetcode 1070. 产品销售分析 III
sql·算法·leetcode
凌肖战1 天前
力扣网编程274题:H指数之普通解法(中等)
算法·leetcode
Y1nhl1 天前
力扣_链表_python版本
开发语言·python·算法·leetcode·链表·职场和发展