{"id":6152,"date":"2020-01-27T01:39:57","date_gmt":"2020-01-27T09:39:57","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6152"},"modified":"2020-02-04T22:56:25","modified_gmt":"2020-02-05T06:56:25","slug":"leetcode-1335-minimum-difficulty-of-a-job-schedule","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1335-minimum-difficulty-of-a-job-schedule\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1335. Minimum Difficulty of a Job Schedule"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1335. Minimum Difficulty of a Job Schedule - \u5237\u9898\u627e\u5de5\u4f5c EP302\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/eRBpfoWujQM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You want to schedule a list of jobs in&nbsp;<code>d<\/code>&nbsp;days. Jobs are dependent (i.e To work on the&nbsp;<code>i-th<\/code>&nbsp;job, you have to finish all the jobs&nbsp;<code>j<\/code>&nbsp;where&nbsp;<code>0 &lt;= j &lt; i<\/code>).<\/p>\n\n\n\n<p>You have to finish&nbsp;<strong>at least<\/strong>&nbsp;one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the&nbsp;<code>d<\/code>&nbsp;days. The difficulty of a day is the maximum difficulty of a job done in that day.<\/p>\n\n\n\n<p>Given an array of integers&nbsp;<code>jobDifficulty<\/code>&nbsp;and an integer&nbsp;<code>d<\/code>. The difficulty of the&nbsp;<code>i-th<\/code>&nbsp;job is&nbsp;<code>jobDifficulty[i]<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum difficulty<\/em>&nbsp;of a job schedule. If you cannot find a schedule for the jobs return&nbsp;<strong>-1<\/strong>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/01\/16\/untitled.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> jobDifficulty = [6,5,4,3,2,1], d = 2\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> First day you can finish the first 5 jobs, total difficulty = 6.\nSecond day you can finish the last job, total difficulty = 1.\nThe difficulty of the schedule = 6 + 1 = 7 \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> jobDifficulty = [9,9,9], d = 4\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> jobDifficulty = [1,1,1], d = 3\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The schedule is one job per day. total difficulty will be 3.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> jobDifficulty = [7,1,7,1,7,1], d = 3\n<strong>Output:<\/strong> 15\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> jobDifficulty = [11,111,22,222,33,333,44,444], d = 6\n<strong>Output:<\/strong> 843\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= jobDifficulty.length &lt;= 300<\/code><\/li><li><code>0 &lt;=&nbsp;jobDifficulty[i] &lt;= 1000<\/code><\/li><li><code>1 &lt;= d &lt;= 10<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"580\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/01\/56F9A2C8-3F83-4C84-880B-F7551A06029D-1024x580.jpeg\" alt=\"\" class=\"wp-image-6157\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/01\/56F9A2C8-3F83-4C84-880B-F7551A06029D-1024x580.jpeg 1024w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/01\/56F9A2C8-3F83-4C84-880B-F7551A06029D-300x170.jpeg 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/01\/56F9A2C8-3F83-4C84-880B-F7551A06029D-768x435.jpeg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>dp[i][k] := min difficulties to schedule jobs 1~i in k days.<\/p>\n\n\n\n<p>Schedule 1 ~ j in k &#8211; 1 days and schedule j + 1 ~ i in 1 day.<\/p>\n\n\n\n<p>Init: dp[0][0] = 0<br>Transition: dp[i][k] := min(dp[j][k -1] + max(jobs[j + 1 ~ i]), k &#8211; 1 &lt;= j &lt; i<br>Answer: dp[n][d]<\/p>\n\n\n\n<p>Time complexity: O(n^2*d)<br>Space complexity: O(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 minDifficulty(vector<int>& jobs, int d) {\n    const int n = jobs.size();\n    if (d > n) return -1;    \n    vector<vector<int>> dp(n + 1, vector<int>(d + 1, INT_MAX \/ 2));\n    \n    dp[0][0] = 0;\n    for (int i = 1; i <= n; ++i)      \n      for (int j = i - 1, md = 0; j >= 0; --j) {\n        md = max(md, jobs[j]);\n        for (int k = 1; k <= min(i, d); ++k)                    \n          dp[i][k] = min(dp[i][k], dp[j][k - 1] + md);\n      }\n    \n    return dp[n][d];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You want to schedule a list of jobs in&nbsp;d&nbsp;days. Jobs are dependent (i.e To work on the&nbsp;i-th&nbsp;job, you have to finish all the jobs&nbsp;j&nbsp;where&nbsp;0 &lt;=&#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":[18,316,217,539],"class_list":["post-6152","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-groups","tag-hard","tag-on2k","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6152","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=6152"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6152\/revisions"}],"predecessor-version":[{"id":6263,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6152\/revisions\/6263"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}