{"id":2795,"date":"2018-04-29T14:00:03","date_gmt":"2018-04-29T21:00:03","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2795"},"modified":"2018-05-18T20:31:53","modified_gmt":"2018-05-19T03:31:53","slug":"leetcode-826-most-profit-assigning-work","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-826-most-profit-assigning-work\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 826. Most Profit Assigning Work"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 826. Most Profit Assigning Work - \u5237\u9898\u627e\u5de5\u4f5c EP188\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/hh1hF2hS3C4?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><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>We have jobs:\u00a0<code>difficulty[i]<\/code>\u00a0is the difficulty of the\u00a0<code>i<\/code>th job, and\u00a0<code>profit[i]<\/code>\u00a0is the profit of the\u00a0<code>i<\/code>th job.<\/p>\n<p>Now we have some workers.\u00a0<code>worker[i]<\/code>\u00a0is the ability of the\u00a0<code>i<\/code>th worker, which means that this worker can only complete a job with difficulty at most\u00a0<code>worker[i]<\/code>.<\/p>\n<p>Every worker can be assigned at most one job, but one job\u00a0can be completed multiple times.<\/p>\n<p>For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.\u00a0 If a worker cannot complete any job, his profit is $0.<\/p>\n<p>What is the most profit we can make?<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]\r\n<strong>Output: <\/strong>100 \r\n<strong>Explanation: W<\/strong>orkers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately.<\/pre>\n<p><strong>Notes:<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= difficulty.length = profit.length &lt;= 10000<\/code><\/li>\n<li><code>1 &lt;= worker.length &lt;= 10000<\/code><\/li>\n<li><code>difficulty[i], profit[i], worker[i]<\/code>\u00a0 are in range\u00a0<code>[1, 10^5]<\/code><\/li>\n<\/ul>\n<h1><strong>Solution 1: Sorting + Two pointers<\/strong><\/h1>\n<p>Time complexity: O(nlogn + mlogm)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 90 ms\r\nclass Solution {\r\npublic:\r\n  int maxProfitAssignment(vector&lt;int&gt;&amp; difficulty, vector&lt;int&gt;&amp; profit, vector&lt;int&gt;&amp; worker) {\r\n    const int n = difficulty.size();\r\n    vector&lt;pair&lt;int, int&gt;&gt; jobs; \/\/ difficulty, profit\r\n    \r\n    for (int i = 0; i &lt; n; ++i)\r\n      jobs.emplace_back(difficulty[i], profit[i]);\r\n    \r\n    std::sort(jobs.begin(), jobs.end());\r\n    std::sort(worker.begin(), worker.end());\r\n    \r\n    int ans = 0;\r\n    int i = 0;\r\n    int best = 0;\r\n    for (int level : worker) {\r\n      while (i &lt; n &amp;&amp; level &gt;= jobs[i].first)\r\n        best = max(best, jobs[i++].second);\r\n      ans += best;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<h1>Solution 2: Bucket + Greedy<\/h1>\n<p>Key idea: for each difficulty D, find the most profit job whose requirement is &lt;= D.<\/p>\n<p>Three steps:<\/p>\n<ol>\n<li>for each difficulty D,\u00a0find the most profit job whose requirement is == D, best[D] = max{profit of\u00a0difficulty D}.<\/li>\n<li>if difficulty D &#8211; 1 can make more profit than\u00a0difficulty D, best[D] =\u00a0max(best[D], best[D &#8211; 1]).<\/li>\n<li>The max profit each worker at skill level D can make is best[D].<\/li>\n<\/ol>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(10000)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 112 ms\r\nclass Solution {\r\npublic:\r\n  int maxProfitAssignment(vector&lt;int&gt;&amp; difficulty, vector&lt;int&gt;&amp; profit, vector&lt;int&gt;&amp; worker) {\r\n    const int N = 100000;\r\n    \/\/ max profit at difficulty i\r\n    vector&lt;int&gt; max_profit(N + 1, 0);\r\n    for (int i = 0; i &lt; difficulty.size(); ++i)\r\n      max_profit[difficulty[i]] = max(max_profit[difficulty[i]], profit[i]);\r\n    for (int i = 2; i &lt;= N; ++i)\r\n      max_profit[i] = max(max_profit[i], max_profit[i - 1]);\r\n    int ans = 0;\r\n    for (int level : worker)\r\n      ans += max_profit[level];\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>C++ using map<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 112 ms\r\nclass Solution {\r\npublic:\r\n  int maxProfitAssignment(vector&lt;int&gt;&amp; difficulty, vector&lt;int&gt;&amp; profit, vector&lt;int&gt;&amp; worker) {\r\n    map&lt;int, int&gt; bests;\r\n    for (int i = 0; i &lt; difficulty.size(); ++i)\r\n      bests[difficulty[i]] = max(bests[difficulty[i]], profit[i]);\r\n    int best = 0;\r\n    for (auto it = bests.begin(); it != bests.end(); ++it)\r\n      it-&gt;second = best = max(it-&gt;second, best);    \r\n    int ans = 0;\r\n    for (int level : worker)\r\n      ans += prev(bests.upper_bound(level))-&gt;second;\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem We have jobs:\u00a0difficulty[i]\u00a0is the difficulty of the\u00a0ith job, and\u00a0profit[i]\u00a0is the profit of the\u00a0ith job. Now we have some workers.\u00a0worker[i]\u00a0is the ability of the\u00a0ith worker,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[300,18,88,177,205],"class_list":["post-2795","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-assignment","tag-dp","tag-greedy","tag-medium","tag-profit","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2795","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=2795"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2795\/revisions"}],"predecessor-version":[{"id":2837,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2795\/revisions\/2837"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}