|
| 1 | +--- |
| 2 | +title: 2054.两个最好的不重叠活动: |
| 3 | +date: 2025-12-23 19:00:09 |
| 4 | +tags: [题解, LeetCode, 中等, 数组, 二分查找, 动态规划, 排序, 堆(优先队列)] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +--- |
| 7 | + |
| 8 | +# 【LetMeFly】2054.两个最好的不重叠活动: |
| 9 | + |
| 10 | +力扣题目链接:[https://leetcode.cn/problems/two-best-non-overlapping-events/](https://leetcode.cn/problems/two-best-non-overlapping-events/) |
| 11 | + |
| 12 | +<p>给你一个下标从 <strong>0</strong> 开始的二维整数数组 <code>events</code> ,其中 <code>events[i] = [startTime<sub>i</sub>, endTime<sub>i</sub>, value<sub>i</sub>]</code> 。第 <code>i</code> 个活动开始于 <code>startTime<sub>i</sub></code> ,结束于 <code>endTime<sub>i</sub></code> ,如果你参加这个活动,那么你可以得到价值 <code>value<sub>i</sub></code> 。你 <strong>最多</strong> 可以参加 <strong>两个时间不重叠</strong> 活动,使得它们的价值之和 <strong>最大</strong> 。</p> |
| 13 | + |
| 14 | +<p>请你返回价值之和的 <strong>最大值</strong> 。</p> |
| 15 | + |
| 16 | +<p>注意,活动的开始时间和结束时间是 <strong>包括</strong> 在活动时间内的,也就是说,你不能参加两个活动且它们之一的开始时间等于另一个活动的结束时间。更具体的,如果你参加一个活动,且结束时间为 <code>t</code> ,那么下一个活动必须在 <code>t + 1</code> 或之后的时间开始。</p> |
| 17 | + |
| 18 | +<p> </p> |
| 19 | + |
| 20 | +<p><strong>示例 1:</strong></p> |
| 21 | + |
| 22 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2021/09/21/picture5.png" style="width: 400px; height: 75px;"></p> |
| 23 | + |
| 24 | +<pre><b>输入:</b>events = [[1,3,2],[4,5,2],[2,4,3]] |
| 25 | +<b>输出:</b>4 |
| 26 | +<strong>解释:</strong>选择绿色的活动 0 和 1 ,价值之和为 2 + 2 = 4 。 |
| 27 | +</pre> |
| 28 | + |
| 29 | +<p><strong>示例 2:</strong></p> |
| 30 | + |
| 31 | +<p><img alt="Example 1 Diagram" src="https://assets.leetcode.com/uploads/2021/09/21/picture1.png" style="width: 400px; height: 77px;"></p> |
| 32 | + |
| 33 | +<pre><b>输入:</b>events = [[1,3,2],[4,5,2],[1,5,5]] |
| 34 | +<b>输出:</b>5 |
| 35 | +<strong>解释:</strong>选择活动 2 ,价值和为 5 。 |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p><strong>示例 3:</strong></p> |
| 39 | + |
| 40 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2021/09/21/picture3.png" style="width: 400px; height: 66px;"></p> |
| 41 | + |
| 42 | +<pre><b>输入:</b>events = [[1,5,3],[1,5,1],[6,6,5]] |
| 43 | +<b>输出:</b>8 |
| 44 | +<strong>解释:</strong>选择活动 0 和 2 ,价值之和为 3 + 5 = 8 。</pre> |
| 45 | + |
| 46 | +<p> </p> |
| 47 | + |
| 48 | +<p><strong>提示:</strong></p> |
| 49 | + |
| 50 | +<ul> |
| 51 | + <li><code>2 <= events.length <= 10<sup>5</sup></code></li> |
| 52 | + <li><code>events[i].length == 3</code></li> |
| 53 | + <li><code>1 <= startTime<sub>i</sub> <= endTime<sub>i</sub> <= 10<sup>9</sup></code></li> |
| 54 | + <li><code>1 <= value<sub>i</sub> <= 10<sup>6</sup></code></li> |
| 55 | +</ul> |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +## 解题方法:xx |
| 60 | + |
| 61 | +11111 |
| 62 | + |
| 63 | ++ 时间复杂度$O(N^2)$ |
| 64 | ++ 空间复杂度$O(N\log N)$ |
| 65 | + |
| 66 | +### AC代码 |
| 67 | + |
| 68 | +#### C++ |
| 69 | + |
| 70 | +```cpp |
| 71 | +/* |
| 72 | + * @Author: LetMeFly |
| 73 | + * @Date: 2025-12-23 13:34:22 |
| 74 | + * @LastEditors: LetMeFly.xyz |
| 75 | + * @LastEditTime: 2025-12-23 18:58:01 |
| 76 | + */ |
| 77 | +#if defined(_WIN32) || defined(__APPLE__) |
| 78 | +#include "_[1,2]toVector.h" |
| 79 | +#endif |
| 80 | + |
| 81 | +class Solution { |
| 82 | +public: |
| 83 | + int maxTwoEvents(vector<vector<int>>& events) { |
| 84 | + sort(events.begin(), events.end(), [](const vector<int>& a, const vector<int>& b) { |
| 85 | + return a[1] < b[1]; |
| 86 | + }); |
| 87 | + vector<pair<int, int>> maxValue; |
| 88 | + int singleMax = 0, pairMax = 0; |
| 89 | + for (vector<int>& e : events) { |
| 90 | + vector<pair<int, int>>::iterator it = lower_bound(maxValue.begin(), maxValue.end(), e[0], [](const pair<int, int>& p, int value) { |
| 91 | + return p.first < value; |
| 92 | + }); |
| 93 | + if (it != maxValue.begin()) { |
| 94 | + pairMax = max(pairMax, (--it)->second + e[2]); |
| 95 | + } |
| 96 | + singleMax = max(singleMax, e[2]); |
| 97 | + maxValue.push_back({e[1], singleMax}); |
| 98 | + } |
| 99 | + return max(pairMax, singleMax); |
| 100 | + } |
| 101 | +}; |
| 102 | +``` |
| 103 | +
|
| 104 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/--------------------------)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/12/23/LeetCode%202054.%E4%B8%A4%E4%B8%AA%E6%9C%80%E5%A5%BD%E7%9A%84%E4%B8%8D%E9%87%8D%E5%8F%A0%E6%B4%BB%E5%8A%A8/)哦~ |
| 105 | +> |
| 106 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
0 commit comments