{"id":7975,"date":"2021-01-10T11:30:53","date_gmt":"2021-01-10T19:30:53","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7975"},"modified":"2021-01-10T19:08:08","modified_gmt":"2021-01-11T03:08:08","slug":"leetcode-1723-find-minimum-time-to-finish-all-jobs","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1723-find-minimum-time-to-finish-all-jobs\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1723. Find Minimum Time to Finish All Jobs"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>jobs<\/code>, where&nbsp;<code>jobs[i]<\/code>&nbsp;is the amount of time it takes to complete the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;job.<\/p>\n\n\n\n<p>There are&nbsp;<code>k<\/code>&nbsp;workers that you can assign jobs to. Each job should be assigned to&nbsp;<strong>exactly<\/strong>&nbsp;one worker. The&nbsp;<strong>working time<\/strong>&nbsp;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&nbsp;<strong>maximum working time<\/strong>&nbsp;of any worker is&nbsp;<strong>minimized<\/strong>.<\/p>\n\n\n\n<p><em>Return the&nbsp;<strong>minimum<\/strong>&nbsp;possible&nbsp;<strong>maximum working time<\/strong>&nbsp;of any assignment.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> jobs = [3,2,3], k = 3\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> By assigning each person one job, the maximum time is 3.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> jobs = [1,2,4,7,8], k = 2\n<strong>Output:<\/strong> 11\n<strong>Explanation:<\/strong> Assign the jobs the following way:\nWorker 1: 1, 2, 8 (working time = 1 + 2 + 8 = 11)\nWorker 2: 4, 7 (working time = 4 + 7 = 11)\nThe maximum working time is 11.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= k &lt;= jobs.length &lt;= 12<\/code><\/li><li><code>1 &lt;= jobs[i] &lt;= 10<sup>7<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: All subsets<\/strong><\/h2>\n\n\n\n<p>dp[i][t] := min of max working time by assigning a subset of jobs s to the first i workers.<\/p>\n\n\n\n<p>dp[i][t] = min{max(dp[i &#8211; 1][s], cost[s ^ t])} where s is a subset of t.<\/p>\n\n\n\n<p>Time complexity: O(k*3^n)<br>Space complexity: O(k*2^n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minimumTimeRequired(vector<int>& jobs, int k) {    \n    const int n = jobs.size();\n    int dp[13][4096];\n    memset(dp, 0x7f, sizeof(dp));\n    dp[0][0] = 0;\n    \n    vector<int> cost(1 << n);\n    for (int t = 0; t < 1 << n; ++t) {\n      for (int j = 0; j < n; ++j)\n        if (t >> j & 1) cost[t] += jobs[j];\n      dp[1][t] = cost[t]; \/\/ do jobs t by one worker\n    }\n    \n    for (int i = 2; i <= k; ++i)\n      for (int t = 0; t < 1 << n; ++t)\n        for (int s = t; s; s = (s - 1) &#038; t)\n          dp[i][t] = min(dp[i][t], max(dp[i - 1][s], cost[t ^ s]));\n    return dp[k][(1 << n) - 1];\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Search + Pruning<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(k^n) <br>Space complexity: O(k*n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int minimumTimeRequired(vector<int>& jobs, int k) {\n    vector<int> times(k);\n    int ans = INT_MAX;\n    function<void(int, int)> dfs = [&](int i, int cur) {\n      if (cur >= ans) return;\n      if (i == jobs.size()) {\n        ans = cur;\n        return;\n      }\n      unordered_set<int> seen;\n      for (int j = 0; j < k; ++j) {\n        if (!seen.insert(times[j]).second) continue;\n        times[j] += jobs[i];\n        dfs(i + 1, max(cur, times[j]));\n        times[j] -= jobs[i];\n      }\n    };\n    sort(rbegin(jobs), rend(jobs));\n    dfs(0, 0);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;jobs, where&nbsp;jobs[i]&nbsp;is the amount of time it takes to complete the&nbsp;ith&nbsp;job. There are&nbsp;k&nbsp;workers that you can assign jobs to. Each&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[687,621,18],"class_list":["post-7975","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-all-subsets","tag-bitmask","tag-dp","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7975","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7975"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7975\/revisions"}],"predecessor-version":[{"id":7981,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7975\/revisions\/7981"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}