返回介绍

solution / 1700-1799 / 1723.Find Minimum Time to Finish All Jobs / README_EN

发布于 2024-06-17 01:03:15 字数 4689 浏览 0 评论 0 收藏 0

1723. Find Minimum Time to Finish All Jobs

中文文档

Description

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

Solutions

Solution 1

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
class Solution {
  private int[] cnt;
  private int ans;
  private int[] jobs;
  private int k;

  public int minimumTimeRequired(int[] jobs, int k) {
    this.k = k;
    Arrays.sort(jobs);
    for (int i = 0, j = jobs.length - 1; i < j; ++i, --j) {
      int t = jobs[i];
      jobs[i] = jobs[j];
      jobs[j] = t;
    }
    this.jobs = jobs;
    cnt = new int[k];
    ans = 0x3f3f3f3f;
    dfs(0);
    return ans;
  }

  private void dfs(int i) {
    if (i == jobs.length) {
      int mx = 0;
      for (int v : cnt) {
        mx = Math.max(mx, v);
      }
      ans = Math.min(ans, mx);
      return;
    }
    for (int j = 0; j < k; ++j) {
      if (cnt[j] + jobs[i] >= ans) {
        continue;
      }
      cnt[j] += jobs[i];
      dfs(i + 1);
      cnt[j] -= jobs[i];
      if (cnt[j] == 0) {
        break;
      }
    }
  }
}
class Solution {
public:
  int ans;

  int minimumTimeRequired(vector<int>& jobs, int k) {
    vector<int> cnt(k);
    ans = 0x3f3f3f3f;
    sort(jobs.begin(), jobs.end(), greater<int>());
    dfs(0, k, jobs, cnt);
    return ans;
  }

  void dfs(int i, int k, vector<int>& jobs, vector<int>& cnt) {
    if (i == jobs.size()) {
      ans = min(ans, *max_element(cnt.begin(), cnt.end()));
      return;
    }
    for (int j = 0; j < k; ++j) {
      if (cnt[j] + jobs[i] >= ans) continue;
      cnt[j] += jobs[i];
      dfs(i + 1, k, jobs, cnt);
      cnt[j] -= jobs[i];
      if (cnt[j] == 0) break;
    }
  }
};
func minimumTimeRequired(jobs []int, k int) int {
  cnt := make([]int, k)
  ans := 0x3f3f3f3f
  sort.Slice(jobs, func(i, j int) bool {
    return jobs[i] > jobs[j]
  })
  var dfs func(int)
  dfs = func(i int) {
    if i == len(jobs) {
      mx := slices.Max(cnt)
      ans = min(ans, mx)
      return
    }
    for j := 0; j < k; j++ {
      if cnt[j]+jobs[i] >= ans {
        continue
      }
      cnt[j] += jobs[i]
      dfs(i + 1)
      cnt[j] -= jobs[i]
      if cnt[j] == 0 {
        break
      }
    }
  }
  dfs(0)
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文