LeetCode 题解工作台

完成所有工作的最短时间

给你一个整数数组 jobs ,其中 jobs[i] 是完成第 i 项工作要花费的时间。 请你将这些工作分配给 k 位工人。所有工作都应该分配给工人,且每项工作只能分配给一位工人。工人的 工作时间 是完成分配给他们的所有工作花费时间的总和。请你设计一套最佳的工作分配方案,使工人的 最大工作时间 得以 …

category

5

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

困难 · 状态·转移·动态规划

bolt

答案摘要

本题与 [2305. 公平分发饼干](https://github.com/doocs/leetcode/blob/main/solution/2300-2399/2305.Fair%20Distribution%20of%20Cookies/README.md) 基本一致,不同的地方仅在于 值的大小。 剪枝优化:优化分配花费时间较大的工作,因此可以先对 按照降序排列。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数数组 jobs ,其中 jobs[i] 是完成第 i 项工作要花费的时间。

请你将这些工作分配给 k 位工人。所有工作都应该分配给工人,且每项工作只能分配给一位工人。工人的 工作时间 是完成分配给他们的所有工作花费时间的总和。请你设计一套最佳的工作分配方案,使工人的 最大工作时间 得以 最小化

返回分配方案中尽可能 最小最大工作时间

 

示例 1:

输入:jobs = [3,2,3], k = 3
输出:3
解释:给每位工人分配一项工作,最大工作时间是 3 。

示例 2:

输入:jobs = [1,2,4,7,8], k = 2
输出:11
解释:按下述方式分配工作:
1 号工人:1、2、8(工作时间 = 1 + 2 + 8 = 11)
2 号工人:4、7(工作时间 = 4 + 7 = 11)
最大工作时间是 11 。

 

提示:

  • 1 <= k <= jobs.length <= 12
  • 1 <= jobs[i] <= 107
lightbulb

解题思路

方法一:DFS + 剪枝

本题与 2305. 公平分发饼干 基本一致,不同的地方仅在于 kk 值的大小。

剪枝优化:优化分配花费时间较大的工作,因此可以先对 jobsjobs 按照降序排列。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
    def minimumTimeRequired(self, jobs: List[int], k: int) -> int:
        def dfs(i):
            nonlocal ans
            if i == len(jobs):
                ans = min(ans, max(cnt))
                return
            for j in range(k):
                if cnt[j] + jobs[i] >= ans:
                    continue
                cnt[j] += jobs[i]
                dfs(i + 1)
                cnt[j] -= jobs[i]
                if cnt[j] == 0:
                    break

        cnt = [0] * k
        jobs.sort(reverse=True)
        ans = inf
        dfs(0)
        return ans
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Candidates should demonstrate understanding of dynamic programming with bitmasking.

  • question_mark

    Candidates should be able to explain the concept of state transitions and how they apply to this problem.

  • question_mark

    Candidates should discuss how backtracking can optimize the solution and how it interacts with dynamic programming.

warning

常见陷阱

外企场景
  • error

    Not considering all subsets of jobs, leading to suboptimal assignments.

  • error

    Overcomplicating the problem by trying to brute force the solution without using dynamic programming or backtracking.

  • error

    Incorrectly transitioning between states or using the wrong bitmask representation for job assignments.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Increasing the number of workers (k) and ensuring the algorithm still performs efficiently.

  • arrow_right_alt

    Using additional constraints like worker availability times or different job priorities.

  • arrow_right_alt

    Considering cases with extremely large job times to test the algorithm’s efficiency.

help

常见问题

外企场景

完成所有工作的最短时间题解:状态·转移·动态规划 | LeetCode #1723 困难