Solving Leetcode Interviews in Seconds with AI: Two Best Non-Overlapping Events
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2054" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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 <= 105 events[i].length == 3 1 <= startTimei <= endTimei <= 109 1 <= valuei <= 106
Explanation
Here's an efficient solution to the problem, along with explanations:
Sort by End Time: Sort the events based on their end times. This allows us to efficiently find non-overlapping events.
Dynamic Programming with Binary Search: For each event, we want to find the best event that ends before it, without overlapping. We use binary search to find the latest event that ends before the current event starts. We store the maximum value achievable up to each event in a DP array.
Maximize Result: Iterate through the events and calculate the maximum sum by either taking the current event's value alone or adding it to the maximum value achievable from a non-overlapping previous event (found using binary search).
Runtime Complexity: O(N log N) due to sorting and binary search. Storage Complexity: O(N) for the DP array.
Code
def max_two_events(events):
"""
Finds the maximum sum of values from at most two non-overlapping events.
Args:
events: A list of events, where each event is a list [startTime, endTime, value].
Returns:
The maximum sum of values from at most two non-overlapping events.
"""
events.sort(key=lambda x: x[1]) # Sort by end time
n = len(events)
dp = [0] * n # dp[i] stores the max value achievable up to event i
dp[0] = events[0][2]
max_value = events[0][2]
def binary_search(end_time):
"""
Finds the latest event that ends before the given end_time.
"""
left, right = 0, n - 1
result = -1
while left <= right:
mid = (left + right) // 2
if events[mid][1] < end_time:
result = mid
left = mid + 1
else:
right = mid - 1
return result
for i in range(1, n):
current_value = events[i][2]
prev_event_index = binary_search(events[i][0])
if prev_event_index != -1:
current_value += dp[prev_event_index]
dp[i] = max(dp[i - 1], current_value, events[i][2]) # max value up to i or just the current event value
max_value = max(max_value, dp[i]) # Keep track of the overall maximum value
return max_value