{"id":5779,"date":"2019-10-24T07:58:41","date_gmt":"2019-10-24T14:58:41","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5779"},"modified":"2019-10-24T08:11:01","modified_gmt":"2019-10-24T15:11:01","slug":"leetcode-1235-maximum-profit-in-job-scheduling","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1235-maximum-profit-in-job-scheduling\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1235. Maximum Profit in Job Scheduling"},"content":{"rendered":"\n<p>We have&nbsp;<code>n<\/code>&nbsp;jobs, where every job&nbsp;is scheduled to be done from&nbsp;<code>startTime[i]<\/code>&nbsp;to&nbsp;<code>endTime[i]<\/code>, obtaining a profit&nbsp;of&nbsp;<code>profit[i]<\/code>.<\/p>\n\n\n\n<p>You&#8217;re given the&nbsp;<code>startTime<\/code>&nbsp;,&nbsp;<code>endTime<\/code>&nbsp;and&nbsp;<code>profit<\/code>&nbsp;arrays,&nbsp;you need to output the maximum profit you can take such that there are no 2 jobs in the subset&nbsp;with overlapping time range.<\/p>\n\n\n\n<p>If you choose a job that ends at time&nbsp;<code>X<\/code>&nbsp;you&nbsp;will be able to start another job that starts at time&nbsp;<code>X<\/code>.<\/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\/2019\/10\/10\/sample1_1584.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70]\n<strong>Output:<\/strong> 120\n<strong>Explanation:<\/strong> The subset chosen is the first and fourth job. \nTime range [1-3]+[3-6] , we get profit of 120 = 50 + 70.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/10\/10\/sample22_1584.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>\nInput:<\/strong> startTime = [1,2,3,4,6], endTime = [3,5,10,6,9], profit = [20,20,100,70,60]\n<strong>Output:<\/strong> 150\n<strong>Explanation:<\/strong> The subset chosen is the first, fourth and fifth job. \nProfit obtained 150 = 20 + 70 + 60.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/10\/10\/sample3_1584.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> startTime = [1,1,1], endTime = [2,3,4], profit = [5,6,4]\n<strong>Output:<\/strong> 6\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= startTime.length == endTime.length ==&nbsp;profit.length&nbsp;&lt;= 5 * 10^4<\/code><\/li><li><code>1 &lt;=&nbsp;startTime[i] &lt;&nbsp;endTime[i] &lt;= 10^9<\/code><\/li><li><code>1 &lt;=&nbsp;profit[i] &lt;= 10^4<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong> + <strong>binary search<\/strong><\/h2>\n\n\n\n<p>Sort jobs by ending time.<br>dp[t] := max profit by end time t.<br><br>for a job = (s, e, p)<br>dp[e] = dp[u] + p, u &lt;= s, and if dp[u] + p &gt; last_element in dp.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<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\nclass Solution {\npublic:\n  int jobScheduling(vector<int>& startTime, vector<int>& endTime, vector<int>& profit) {\n    const int n = startTime.size();  \n    vector<vector<int>> jobs(n);\n    for (int i = 0; i < n; ++i)\n      jobs[i] = {endTime[i], startTime[i], profit[i]};\n    sort(begin(jobs), end(jobs));\n    \n    int ans = 0;\n    map<int, int> m{{0, 0}}; \/\/ {end_time -> max_profit}\n    for (const auto& job : jobs) {\n      int p = prev(m.upper_bound(job[1]))->second + job[2];\n      if (p > rbegin(m)->second) {\n        m[job[0]] = p;\n      }      \n    }\n    return rbegin(m)->second;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We have&nbsp;n&nbsp;jobs, where every job&nbsp;is scheduled to be done from&nbsp;startTime[i]&nbsp;to&nbsp;endTime[i], obtaining a profit&nbsp;of&nbsp;profit[i]. You&#8217;re given the&nbsp;startTime&nbsp;,&nbsp;endTime&nbsp;and&nbsp;profit&nbsp;arrays,&nbsp;you need to output the maximum profit you can take&#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":[52,18,217],"class_list":["post-5779","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-binary-search","tag-dp","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5779","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=5779"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5779\/revisions"}],"predecessor-version":[{"id":5781,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5779\/revisions\/5781"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}