You are given a 2D integer array intervals, where intervals[i] = [li, ri, weighti]. Interval i starts at position li and ends at ri, and has a weight of weighti. 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-overlapping if they do not share any points. In particular, intervals sharing a left or right boundary are considered overlapping.
We pick at most four non-overlapping weighted intervals to maximize the total weight, breaking ties by the lexicographically smallest index tuple. \(n\le 5\times 10^4\) forbids subset search.
This is weighted interval scheduling with a cap of four. After sorting by left endpoint, the next non-overlapping interval is a binary search.
State \((i,k)\) starts at interval \(i\) with \(k\) picks remaining. We either skip \(i\) or take it and jump to \(\textit{nxt}[i]\), comparing both weight and the index list so the lexicographically smallest optimum is kept.
Copy the intervals and record each original index, then sort by left endpoint. For each interval \(i\), binary-search the first position \(\textit{nxt}[i]\) whose left endpoint is strictly greater than \(i\)'s right endpoint (shared endpoints count as overlap).
Let \(f[i][k]\) be the maximum weight obtainable from interval \(i\) onward with at most \(k\) picks, and let \(g[i][k]\) store the corresponding lexicographically smallest index list. Transition from the back: skipping \(i\) inherits \(f[i+1][k]\); taking \(i\) inserts its original index into \(g[\textit{nxt}[i]][k-1]\) and adds the current weight. Keep the larger weight, or the lexicographically smaller index list on a tie. The answer is \(g[0][4]\).
The time complexity is \(O(n \times \log n)\) and the space complexity is \(O(n)\). At most \(4\) intervals are chosen, so inserting and comparing index lists is constant time.