1723. Find Minimum Time to Finish All Jobs

87 篇文章 0 订阅
65 篇文章 0 订阅

You are given an integer array jobs, where jobs[i] is the amount of time it takes 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 takes to complete all jobs assigned to them. Your goal is to devise an optimal assignment 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

Contest完整题解可见公众号,欢迎关注:https://mp.weixin.qq.com/s?__biz=MzIwMTk4MzkyOA==&tempkey=MTA5NV8wSGY1bXdvRTUzQnZOb0NYc1N4c0FHWUJhQlNiSEU1NWQ1Rm1PdmtTeXhiQkNOTURIbVpwX1NZMkd2YVFDSDY3UGJ0Sno2eU1oLXVMdVRQOHZMTjJHd043ckFYaFZiVmI3TFdOMkpld0JaUVpkeXJyV0FXLW5sX3cyYWc0eUdIbERtdnRvRkJoRjY2SDNWT0hkbndkcDZ5aW9IRGdJN0ZpRkRHaDhnfn4%3D&chksm=16e4d0d2219359c4352c2b28fec53b58767fe6983b39bae2928fa95bbacd425676f1185f35b0#rd

思路:

DFS+Prune

class Solution:
    def minimumTimeRequired(self, jobs: List[int], k: int) -> int:
        workers = [0]*k
        
        self.res = sys.maxsize
        # jobs.sort(reverse = True)
        def dfs(curr):
            if curr == len(jobs):
                self.res = min(self.res, max(workers))
                return
            
            seen = set() # record searched workload of workers
            for i in range(k):
                if workers[i] in seen: continue # if we have searched the workload of 5, skip it.
                if workers[i] + jobs[curr] >= self.res: continue # another branch cutting
                seen.add(workers[i])
                workers[i] += jobs[curr]
                dfs(curr+1)
                workers[i] -= jobs[curr]
        
        dfs(0)
        return self.res

二分+DFS+Prune

class Solution(object):
    def minimumTimeRequired(self, jobs, K):
        """
        :type jobs: List[int]
        :type k: int
        :rtype: int
        """
        jobs.sort(reverse=True)
        def ok(mid, status, s):
            if s == len(jobs):
                return True
            for i in range(K):
                if status[i]+jobs[s] <= mid:
                    status[i] += jobs[s]
                    if ok(mid, status, s+1):
                        return True
                    status[i] -= jobs[s]
                if status[i] == 0:
                    return False
            return False

        lo,hi = 0,sum(jobs)
        res = sum(jobs)
        while lo<=hi:
            mid = (lo+hi)//2
            status = [0]*K
            if ok(mid, status, 0):
                res = mid
                hi = mid-1
            else:
                lo = mid+1
        return res

状压DP,Python会TLE,倒是学到了用 (t-1)& t  来求小于t的sub-binary string(原来是0的地方还是0)对应的值。比如6的二进制是110,小于6的sub-binary二进制是100,对应4 = 110 &(110-1)

class Solution(object):
    def minimumTimeRequired(self, jobs, k):
        """
        :type jobs: List[int]
        :type k: int
        :rtype: int
        """
        n = len(jobs)
        target = 1<<n
        helper = [0] * target
        for i in range(target):
            for bit in range(n):
                if (1<<bit) & i:
                    helper[i] += jobs[bit]

        dp = [[0 for _ in range(target)] for _ in range(k)]
        dp[0] = helper
        for i in range(1, k):
            for t in range(target):
                dp[i][t] = dp[i-1][t]
                s = t
                while s:
                    dp[i][t] = min(dp[i][t], max(dp[i-1][t-s], helper[s]))
                    s = (s - 1) & t
        return dp[-1][-1]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值