{"id":8071,"date":"2021-02-06T08:37:38","date_gmt":"2021-02-06T16:37:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8071"},"modified":"2021-02-06T10:52:23","modified_gmt":"2021-02-06T18:52:23","slug":"leetcode-1751-maximum-number-of-events-that-can-be-attended-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1751-maximum-number-of-events-that-can-be-attended-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1751. Maximum Number of Events That Can Be Attended II"},"content":{"rendered":"\n<p>You are given an array of&nbsp;<code>events<\/code>&nbsp;where&nbsp;<code>events[i] = [startDay<sub>i<\/sub>, endDay<sub>i<\/sub>, value<sub>i<\/sub>]<\/code>. The&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;event starts at&nbsp;<code>startDay<sub>i<\/sub><\/code>and ends at&nbsp;<code>endDay<sub>i<\/sub><\/code>, and if you attend this event, you will receive a value of&nbsp;<code>value<sub>i<\/sub><\/code>. You are also given an integer&nbsp;<code>k<\/code>&nbsp;which represents the maximum number of events you can attend.<\/p>\n\n\n\n<p>You can only attend one event at a time. If you choose to attend an event, you must attend the&nbsp;<strong>entire<\/strong>&nbsp;event. Note that the end day is&nbsp;<strong>inclusive<\/strong>: that is, you cannot attend two events where one of them starts and the other ends on the same day.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum sum<\/strong>&nbsp;of values that you can receive by attending events.<\/em><\/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\/2021\/01\/10\/screenshot-2021-01-11-at-60048-pm.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> events = [[1,2,4],[3,4,3],[2,3,1]], k = 2\n<strong>Output:<\/strong> 7\n<strong>Explanation: <\/strong>Choose the green events, 0 and 1 (0-indexed) for a total value of 4 + 3 = 7.<\/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\/2021\/01\/10\/screenshot-2021-01-11-at-60150-pm.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> events = [[1,2,4],[3,4,3],[2,3,10]], k = 2\n<strong>Output:<\/strong> 10\n<strong>Explanation:<\/strong> Choose event 2 for a total value of 10.\nNotice that you cannot attend any other event as they overlap, and that you do <strong>not<\/strong> have to attend k events.<\/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\/2021\/01\/10\/screenshot-2021-01-11-at-60703-pm.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> events = [[1,1,1],[2,2,2],[3,3,3],[4,4,4]], k = 3\n<strong>Output:<\/strong> 9\n<strong>Explanation:<\/strong> Although the events do not overlap, you can only attend 3 events. Pick the highest valued three.<\/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;= events.length<\/code><\/li><li><code>1 &lt;= k * events.length &lt;= 10<sup>6<\/sup><\/code><\/li><li><code>1 &lt;= startDay<sub>i<\/sub>&nbsp;&lt;= endDay<sub>i<\/sub>&nbsp;&lt;= 10<sup>9<\/sup><\/code><\/li><li><code>1 &lt;= value<sub>i<\/sub>&nbsp;&lt;= 10<sup>6<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP + Binary Search<\/strong><\/h2>\n\n\n\n<p>Sort events by ending time.<br>dp[i][j] := max value we can get by attending <strong><span class=\"has-inline-color has-vivid-red-color\">at most<\/span><\/strong> j events among events[0~i].<br>dp[i][j] = max(dp[i &#8211; 1][j],  dp[p][j &#8211; 1] + value[i])<br>p is the first event that does not overlap with the current one.<\/p>\n\n\n\n<p>Time complexity: O(nlogn + nk)<br>Space complexity: O(nk)<\/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 maxValue(vector<vector<int>>& events, int k) {\n    const int n = events.size(); \n    vector<vector<int>> dp(n + 1, vector<int>(k + 1));    \n    vector<int> e(n);\n    auto comp = [](const vector<int>& a, const vector<int>& b) {\n      return a[1] < b[1];\n    };\n    \n    sort(begin(events), end(events), comp);\n    \n    for (int i = 1; i <= n; ++i) {\n      const int p = lower_bound(begin(events), \n                                begin(events) + i, \n                                vector<int>{0, events[i - 1][0], 0},\n                                comp) - begin(events);\n      for (int j = 1; j <= k; ++j)\n        dp[i][j] = max(dp[i - 1][j], \n                       dp[p][j - 1] + events[i - 1][2]);\n    }    \n    return dp[n][k];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array of&nbsp;events&nbsp;where&nbsp;events[i] = [startDayi, endDayi, valuei]. The&nbsp;ith&nbsp;event starts at&nbsp;startDayiand ends at&nbsp;endDayi, and if you attend this event, you will receive a&#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,495,18,217,139],"class_list":["post-8071","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-binary-search","tag-date","tag-dp","tag-hard","tag-range","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8071","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=8071"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8071\/revisions"}],"predecessor-version":[{"id":8077,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8071\/revisions\/8077"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}