leetcode - 2054. Two Best Non-Overlapping Events

Description

You are given a 0-indexed 2D integer array of events where events[i] = [startTimei, endTimei, valuei]. The ith event starts at startTimei and ends at endTimei, and if you attend this event, you will receive a value of valuei. You can choose at most two non-overlapping events to attend such that the sum of their values is maximized.

Return this maximum sum.

Note that the start time and end time is inclusive: 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 t, the next event must start at or after t + 1.

Example 1:

复制代码
Input: events = [[1,3,2],[4,5,2],[2,4,3]]
Output: 4
Explanation: Choose the green events, 0 and 1 for a sum of 2 + 2 = 4.

Example 2:

复制代码
Input: events = [[1,3,2],[4,5,2],[1,5,5]]
Output: 5
Explanation: Choose event 2 for a sum of 5.

Example 3:

复制代码
Input: events = [[1,5,3],[1,5,1],[6,6,5]]
Output: 8
Explanation: Choose events 0 and 2 for a sum of 3 + 5 = 8.

Constraints:

复制代码
2 <= events.length <= 10^5
events[i].length == 3
1 <= startTimei <= endTimei <= 10^9
1 <= valuei <= 10^6

Solution

Solved after help...

Heap

Use a min-heap to store events with earlier ending time. When we have a new event, pop from the heap to get non-overlapping events, and stop until the earliest event in heap is over-lapping. Use a variable to store the maximum value during popping, and when we stop, we have a possible maximum pair: with the current one and the maximum one.

We need to sort the events by start time first.

Time complexity: o ( n log ⁡ n ) o(n\log n) o(nlogn)

Space complexity: o ( n ) o(n) o(n)

Greedy

How could someone come up with this solution??? @_@

This is actually similar to heap. So in heap, a key thing we need to do is: use max_val to keep track of all the values of events that end earlier than the current one. So if we flatten the events by its time, and each time when we see a start of an event, we know we could pair it with the most valuable event that ends earlier than it. Each time we see a close of an event, we know we could update the "most valuable event that ends earlier".

To do so, use end_time + 1 as the time for endings, and put it before the start if we have a tie.

Time complexity: o ( n log ⁡ n ) o(n\log n) o(nlogn)

Space complexity: o ( n ) o(n) o(n)

Code

Heap

python3 复制代码
class Solution:
    def maxTwoEvents(self, events: List[List[int]]) -> int:
        # (end_time, val)
        prev_events = []
        events.sort(key=lambda x: x[0])
        res = 0
        max_val = 0
        for start_time, end_time, val in events:
            while prev_events and prev_events[0][0] < start_time:
                _, top_val = heapq.heappop(prev_events)
                max_val = max(max_val, top_val)
            res = max(res, max_val + val)
            heapq.heappush(prev_events, (end_time, val))
        return res

Greedy

python3 复制代码
class Solution:
    def maxTwoEvents(self, events: List[List[int]]) -> int:
        event_items = []
        for start_time, end_time, val in events:
            event_items.append((start_time, 1, val))
            event_items.append((end_time + 1, 0, val))
        event_items.sort()
        res = 0
        max_val = 0
        for event_time, event_type, val in event_items:
            if event_type == 1:
                res = max(res, val + max_val)
            else:
                max_val = max(max_val, val)
        return res
相关推荐
不能只会打代码3 分钟前
力扣--3578. 统计极差最大为 K 的分割方式数(Java实现,代码注释及题目分析讲解)
算法·leetcode·动态规划·滑动窗口
小尧嵌入式5 分钟前
QT软件开发知识流程及秒表计时器开发
开发语言·c++·qt·算法
踢球的打工仔19 分钟前
前端html(3)
前端·算法·html
程序员-King.20 分钟前
day114—同向双指针(数组)—统计得分小于K的子数组数目(LeetCode-2302)
算法·leetcode·双指针
智算菩萨21 分钟前
深度学习在教育数据挖掘(EDM)中的方法体系:从任务建模到算法范式的理论梳理与总结
深度学习·算法·数据挖掘
_OP_CHEN25 分钟前
【算法基础篇】(二十七)从记忆化搜索到动态规划:保姆级入门指南,带你吃透 DP 核心思想!
算法·蓝桥杯·动态规划·记忆化搜索·算法竞赛·acm/icpc
是Dream呀25 分钟前
后端开发入门超完整速成路线(算法篇)
算法
代码雕刻家26 分钟前
1.10.课设实验-数据结构-查找-机票查询
c语言·数据结构·算法
biyezuopinvip32 分钟前
音频DSP技术与应用数字信号处理算法实验(论文)
算法·音视频·信号处理·代码·音频dsp技术·应用数字信号·处理算法实验
小妖66637 分钟前
力扣(LeetCode)- 60. 排列序列
算法·leetcode·职场和发展