{"id":7705,"date":"2020-11-22T20:28:31","date_gmt":"2020-11-23T04:28:31","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7705"},"modified":"2020-11-22T20:30:41","modified_gmt":"2020-11-23T04:30:41","slug":"leetcode-1665-minimum-initial-energy-to-finish-tasks","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1665-minimum-initial-energy-to-finish-tasks\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1665. Minimum Initial Energy to Finish Tasks"},"content":{"rendered":"\n<p>You are given an array&nbsp;<code>tasks<\/code>&nbsp;where&nbsp;<code>tasks[i] = [actual<sub>i<\/sub>, minimum<sub>i<\/sub>]<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>actual<sub>i<\/sub><\/code>&nbsp;is the actual amount of energy you&nbsp;<strong>spend to finish<\/strong>&nbsp;the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;task.<\/li><li><code>minimum<sub>i<\/sub><\/code>&nbsp;is the minimum amount of energy you&nbsp;<strong>require to begin<\/strong>&nbsp;the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;task.<\/li><\/ul>\n\n\n\n<p>For example, if the task is&nbsp;<code>[10, 12]<\/code>&nbsp;and your current energy is&nbsp;<code>11<\/code>, you cannot start this task. However, if your current energy is&nbsp;<code>13<\/code>, you can complete this task, and your energy will be&nbsp;<code>3<\/code>&nbsp;after finishing it.<\/p>\n\n\n\n<p>You can finish the tasks in&nbsp;<strong>any order<\/strong>&nbsp;you like.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;initial amount of energy you will need<\/em>&nbsp;<em>to finish all the tasks<\/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> tasks = [[1,2],[2,4],[4,8]]\n<strong>Output:<\/strong> 8\n<strong>Explanation:<\/strong>\nStarting with 8 energy, we finish the tasks in the following order:\n    - 3rd task. Now energy = 8 - 4 = 4.\n    - 2nd task. Now energy = 4 - 2 = 2.\n    - 1st task. Now energy = 2 - 1 = 1.\nNotice that even though we have leftover energy, starting with 7 energy does not work because we cannot do the 3rd task.<\/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> tasks = [[1,3],[2,4],[10,11],[10,12],[8,9]]\n<strong>Output:<\/strong> 32\n<strong>Explanation:<\/strong>\nStarting with 32 energy, we finish the tasks in the following order:\n    - 1st task. Now energy = 32 - 1 = 31.\n    - 2nd task. Now energy = 31 - 2 = 29.\n    - 3rd task. Now energy = 29 - 10 = 19.\n    - 4th task. Now energy = 19 - 10 = 9.\n    - 5th task. Now energy = 9 - 8 = 1.<\/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> tasks = [[1,7],[2,8],[3,9],[4,10],[5,11],[6,12]]\n<strong>Output:<\/strong> 27\n<strong>Explanation:<\/strong>\nStarting with 27 energy, we finish the tasks in the following order:\n    - 5th task. Now energy = 27 - 5 = 22.\n    - 2nd task. Now energy = 22 - 2 = 20.\n    - 3rd task. Now energy = 20 - 3 = 17.\n    - 1st task. Now energy = 17 - 1 = 16.\n    - 4th task. Now energy = 16 - 4 = 12.\n    - 6th task. Now energy = 12 - 6 = 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;= tasks.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= actual<sub>\u200bi<\/sub>&nbsp;&lt;= minimum<sub>i<\/sub>&nbsp;&lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy + Binary Search<\/strong><\/h2>\n\n\n\n<p>Sort tasks by actual &#8211; min in ascending order, this will be the order we finish those tasks. Use binary search to check whether a given initial energy works or not. Note, the binary search part is unnecessary. <\/p>\n\n\n\n<p>Time complexity: O(nlogn + nlogk)<br>Space complexity: O(1)<\/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 minimumEffort(vector<vector<int>>& tasks) {\n    sort(begin(tasks), end(tasks), [](const auto& t1, const auto& t2) {\n      return t1[0] - t1[1] < t2[0] - t2[1];\n    });\n    int l = tasks[0][1], r = INT_MAX - 1;\n    auto check = [&#038;](int cur) {\n      for (const auto&#038; task : tasks) {        \n        if (task[1] > cur) return false;\n        cur -= task[0];        \n      }\n      return true;\n    };\n    while (l < r) {\n      const int m = l + (r - l) \/ 2;\n      check(m) ? r = m : l = m + 1;\n    }\n    return l;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array&nbsp;tasks&nbsp;where&nbsp;tasks[i] = [actuali, minimumi]: actuali&nbsp;is the actual amount of energy you&nbsp;spend to finish&nbsp;the&nbsp;ith&nbsp;task. minimumi&nbsp;is the minimum amount of energy you&nbsp;require to&#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":[20,88,217],"class_list":["post-7705","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-array","tag-greedy","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7705","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=7705"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7705\/revisions"}],"predecessor-version":[{"id":7708,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7705\/revisions\/7708"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}