Advertisement

Maximum Score of Non-overlapping Intervals - LeetCode 3414 Solution

Maximum Score of Non-overlapping Intervals - Complete Solution Guide

Maximum Score of Non-overlapping Intervals is LeetCode problem 3414, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given a 2D integer array intervals , where intervals[i] = [l i , r i , weight i ] . Interval i starts at position l i and ends at r i , and has a weight of weight i . You can choose up to 4 non-overlapping intervals. The score of the chosen intervals is defined as the total sum of their weights. Return the lexicographically smallest array of at most 4 indices from intervals with maximum score, representing your choice of non-overlapping intervals. Two intervals are said to be non-overlap

Detailed Explanation

The problem requires us to select at most 4 non-overlapping intervals from a given set of intervals, each having a start time, end time, and weight. The goal is to maximize the total weight of the chosen intervals while also ensuring that the selected intervals' indices, when sorted, form the lexicographically smallest array possible in case of ties in total weight.

Solution Approach

The provided solution uses a dynamic programming approach to find the maximum weight of at most 4 non-overlapping intervals. It first sorts the intervals by their start and end times. Then, it uses a DP table to store the maximum weight achievable using up to `k` intervals, ending at a given index. The DP table is built from the end of the sorted intervals to the beginning. A binary search is employed to find the next non-overlapping interval efficiently. When there's a tie in the total weight, the code compares the lists of interval indices lexicographically to choose the smallest one.

Step-by-Step Algorithm

  1. Step 1: Augment each interval with its original index and store this information.
  2. Step 2: Sort the intervals (with indices) by start time, and then by end time in case of ties.
  3. Step 3: Create a DP table `dp[i][k]` where `dp[i][k]` stores a pair: (maximum weight using up to `k` intervals from index `i` onwards, list of interval indices used to achieve the maximum weight).
  4. Step 4: Iterate through the intervals in reverse order (from `n-1` to `0`).
  5. Step 5: For each interval `i`, iterate through the possible number of intervals to choose (`k` from `1` to `4`).
  6. Step 6: Compute the two options: (a) skip interval `i` and take the result from `dp[i+1][k]`, (b) take interval `i`. To take interval `i`, find the next non-overlapping interval `j` using binary search (bisect_right). The weight will then be weight of interval `i` + value of `dp[j][k-1]`.
  7. Step 7: Compare the weight obtained by skipping or taking the interval, and compare their indices lexicographically in case of weight ties. Store the result.
  8. Step 8: The result `dp[0][4]` stores the maximum weight using up to 4 intervals and the lexicographically smallest set of indices.
  9. Step 9: Return the list of indices.

Key Insights

  • Insight 1: Sorting intervals based on start and end times simplifies non-overlapping checks and allows for efficient processing.
  • Insight 2: Dynamic programming is suitable to explore all possible combinations of non-overlapping intervals and their weights.
  • Insight 3: The lexicographical tie-breaker adds complexity, requiring careful comparison of index lists when weights are equal.

Complexity Analysis

Time Complexity: O(n*n*log(n))

Space Complexity: O(n)

Topics

This problem involves: Array, Binary Search, Dynamic Programming, Sorting.

Companies

Asked at: Sprinklr.