leetcode[1723] Find Minimum Time to Finish All Jobs Python3实现(官方题解Python版,二分查找+回溯+剪枝)

# You are given an integer array jobs, where jobs[i] is the amount of time it ta
# kes to complete the ith job. 
# 
#  There are k workers that you can assign jobs to. Each job should be assigned 
# to exactly one worker. The working time of a worker is the sum of the time it ta
# kes to complete all jobs assigned to them. Your goal is to devise an optimal ass
# ignment such that the maximum working time of any worker is minimized. 
# 
#  Return the minimum possible maximum working time of any assignment. 
# 
#  
#  Example 1: 
# 
#  
# Input: jobs = [3,2,3], k = 3
# Output: 3
# Explanation: By assigning each person one job, the maximum time is 3.
#  
# 
#  Example 2: 
# 
#  
# Input: jobs = [1,2,4,7,8], k = 2
# Output: 11
# Explanation: Assign the jobs the following way:
# Worker 1: 1, 2, 8 (working time = 1 + 2 + 8 = 11)
# Worker 2: 4, 7 (working time = 4 + 7 = 11)
# The maximum working time is 11. 
# 
#  
#  Constraints: 
# 
#  
#  1 <= k <= jobs.length <= 12 
#  1 <= jobs[i] <= 107 
#  
#  Related Topics 递归 回溯算法 
#  👍 100 👎 0

from typing import List
# leetcode submit region begin(Prohibit modification and deletion)
class Solution:
    def minimumTimeRequired(self, jobs: List[int], k: int) -> int:
        def backtrack(workloads, idx, limit):
            if idx >= len(jobs):
                return True
            cur = jobs[idx]
            for i in range(len(workloads)):
                if workloads[i] + cur <= limit:
                    workloads[i] += cur
                    if backtrack(workloads, idx+1, limit):
                        return True
                    workloads[i] -= cur # 回溯
                if workloads[i] == 0 or workloads[i] + cur == limit:
                    break
            return False

        def check(k, limit):
            workloads = [0] * k
            return backtrack(workloads, 0, limit)

        jobs.sort(reverse=True)
        l = jobs[0]  # 完成时间下界为时间最长的工作
        r = sum(jobs)  # 完成时间上界为工作时间总和,只分给一个人
        while l < r:
            mid = (l + r) >> 1
            if check(k, mid):
                r = mid
            else:
                l = mid + 1
        return l
# leetcode submit region end(Prohibit modification and deletion)

照搬官方题解,改写为了Python版,二分查找+回溯+剪枝。

类似 1011. 在 D 天内送达包裹的能力

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值