{"id":8653,"date":"2021-11-01T19:16:37","date_gmt":"2021-11-02T02:16:37","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8653"},"modified":"2021-11-03T19:14:04","modified_gmt":"2021-11-04T02:14:04","slug":"leetcode-2054-two-best-non-overlapping-events","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/heap\/leetcode-2054-two-best-non-overlapping-events\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2054. Two Best Non-Overlapping Events"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;2D integer array of&nbsp;<code>events<\/code>&nbsp;where&nbsp;<code>events[i] = [startTime<sub>i<\/sub>, endTime<sub>i<\/sub>, value<sub>i<\/sub>]<\/code>. The&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;event starts at&nbsp;<code>startTime<sub>i<\/sub><\/code>and ends at&nbsp;<code>endTime<sub>i<\/sub><\/code>, and if you attend this event, you will receive a value of&nbsp;<code>value<sub>i<\/sub><\/code>. You can choose&nbsp;<strong>at most<\/strong>&nbsp;<strong>two<\/strong>&nbsp;<strong>non-overlapping<\/strong>&nbsp;events to attend such that the sum of their values is&nbsp;<strong>maximized<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>this&nbsp;<strong>maximum<\/strong>&nbsp;sum.<\/em><\/p>\n\n\n\n<p>Note that the start time and end time is&nbsp;<strong>inclusive<\/strong>: that is, you cannot attend two events where one of them starts and the other ends at the same time. More specifically, if you attend an event with end time&nbsp;<code>t<\/code>, the next event must start at or after&nbsp;<code>t + 1<\/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\/2021\/09\/21\/picture5.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> events = [[1,3,2],[4,5,2],[2,4,3]]\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>Choose the green events, 0 and 1 for a sum of 2 + 2 = 4.\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\/2021\/09\/21\/picture1.png\" alt=\"Example 1 Diagram\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> events = [[1,3,2],[4,5,2],[1,5,5]]\n<strong>Output:<\/strong> 5\n<strong>Explanation: <\/strong>Choose event 2 for a sum of 5.\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\/2021\/09\/21\/picture3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> events = [[1,5,3],[1,5,1],[6,6,5]]\n<strong>Output:<\/strong> 8\n<strong>Explanation: <\/strong>Choose events 0 and 2 for a sum of 3 + 5 = 8.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= events.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>events[i].length == 3<\/code><\/li><li><code>1 &lt;= startTime<sub>i<\/sub>&nbsp;&lt;= endTime<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: Sort + Heap<\/strong><\/h2>\n\n\n\n<p>Sort events by start time, process them from left to right.<\/p>\n\n\n\n<p>Use a min heap to store the events processed so far, a variable cur to track the max value of a non-overlapping event.<\/p>\n\n\n\n<p>For a given event, pop all non-overlapping events whose end time is smaller than its start time and update cur.<\/p>\n\n\n\n<p>ans = max(val + cur)<\/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 maxTwoEvents(vector<vector<int>>& events) {\n    using E = pair<int,int>;\n    sort(begin(events), end(events));\n    priority_queue<E, vector<E>, greater<E>> q; \/\/ (end_time, val)\n    int ans = 0;\n    int cur = 0;\n    for (const auto& e : events) {\n      while (!q.empty() && q.top().first < e[0]) {\n        cur = max(cur, q.top().second);\n        q.pop();\n      }\n      ans = max(ans, cur + e[2]);\n      q.emplace(e[1], e[2]);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;2D integer array of&nbsp;events&nbsp;where&nbsp;events[i] = [startTimei, endTimei, valuei]. The&nbsp;ith&nbsp;event starts at&nbsp;startTimeiand ends at&nbsp;endTimei, and if you attend this event, you will receive&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71],"tags":[724,73,177,23],"class_list":["post-8653","post","type-post","status-publish","format-standard","hentry","category-heap","tag-events","tag-heap","tag-medium","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8653","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=8653"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8653\/revisions"}],"predecessor-version":[{"id":8655,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8653\/revisions\/8655"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}