Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given an array of events where events[i] = [startDayi, endDayi, valuei]. The ith event starts at startDayi and ends at endDayi, and if you attend this event, you will receive a value of valuei. You are also given an integer k which represents the maximum number of events you can attend.
You can only attend one event at a time. If you choose to attend an event, you must attend the entire event. Note that the end day is inclusive: that is, you cannot attend two events where one of them starts and the other ends on the same day.
Return the maximum sum of values that you can receive by attending events.
Example 1:
Input: events = [[1,2,4],[3,4,3],[2,3,1]], k = 2 Output: 7 Explanation: Choose the green events, 0 and 1 (0-indexed) for a total value of 4 + 3 = 7.
Example 2:
Input: events = [[1,2,4],[3,4,3],[2,3,10]], k = 2 Output: 10 Explanation: Choose event 2 for a total value of 10. Notice that you cannot attend any other event as they overlap, and that you do not have to attend k events.
Example 3:
Input: events = [[1,1,1],[2,2,2],[3,3,3],[4,4,4]], k = 3 Output: 9 Explanation: Although the events do not overlap, you can only attend 3 events. Pick the highest valued three.
Constraints:
1 <= k <= events.length1 <= k * events.length <= 1061 <= startDayi <= endDayi <= 1091 <= valuei <= 106Problem summary: You are given an array of events where events[i] = [startDayi, endDayi, valuei]. The ith event starts at startDayi and ends at endDayi, and if you attend this event, you will receive a value of valuei. You are also given an integer k which represents the maximum number of events you can attend. You can only attend one event at a time. If you choose to attend an event, you must attend the entire event. Note that the end day is inclusive: that is, you cannot attend two events where one of them starts and the other ends on the same day. Return the maximum sum of values that you can receive by attending events.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Dynamic Programming
[[1,2,4],[3,4,3],[2,3,1]] 2
[[1,2,4],[3,4,3],[2,3,10]] 2
[[1,1,1],[2,2,2],[3,3,3],[4,4,4]] 3
maximum-number-of-events-that-can-be-attended)maximum-earnings-from-taxi)two-best-non-overlapping-events)meeting-rooms-iii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1751: Maximum Number of Events That Can Be Attended II
class Solution {
private int[][] events;
private int[][] f;
private int n;
public int maxValue(int[][] events, int k) {
Arrays.sort(events, (a, b) -> a[0] - b[0]);
this.events = events;
n = events.length;
f = new int[n][k + 1];
return dfs(0, k);
}
private int dfs(int i, int k) {
if (i >= n || k <= 0) {
return 0;
}
if (f[i][k] != 0) {
return f[i][k];
}
int j = search(events, events[i][1], i + 1);
int ans = Math.max(dfs(i + 1, k), dfs(j, k - 1) + events[i][2]);
return f[i][k] = ans;
}
private int search(int[][] events, int x, int lo) {
int l = lo, r = n;
while (l < r) {
int mid = (l + r) >> 1;
if (events[mid][0] > x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
}
// Accepted solution for LeetCode #1751: Maximum Number of Events That Can Be Attended II
func maxValue(events [][]int, k int) int {
sort.Slice(events, func(i, j int) bool { return events[i][0] < events[j][0] })
n := len(events)
f := make([][]int, n)
for i := range f {
f[i] = make([]int, k+1)
}
var dfs func(i, k int) int
dfs = func(i, k int) int {
if i >= n || k <= 0 {
return 0
}
if f[i][k] > 0 {
return f[i][k]
}
j := sort.Search(n, func(h int) bool { return events[h][0] > events[i][1] })
ans := max(dfs(i+1, k), dfs(j, k-1)+events[i][2])
f[i][k] = ans
return ans
}
return dfs(0, k)
}
# Accepted solution for LeetCode #1751: Maximum Number of Events That Can Be Attended II
class Solution:
def maxValue(self, events: List[List[int]], k: int) -> int:
@cache
def dfs(i: int, k: int) -> int:
if i >= len(events):
return 0
_, ed, val = events[i]
ans = dfs(i + 1, k)
if k:
j = bisect_right(events, ed, lo=i + 1, key=lambda x: x[0])
ans = max(ans, dfs(j, k - 1) + val)
return ans
events.sort()
return dfs(0, k)
// Accepted solution for LeetCode #1751: Maximum Number of Events That Can Be Attended II
impl Solution {
pub fn max_value(mut events: Vec<Vec<i32>>, k: i32) -> i32 {
events.sort_by_key(|e| e[0]);
let n = events.len();
let mut f = vec![vec![0; (k + 1) as usize]; n];
fn dfs(i: usize, k: i32, events: &Vec<Vec<i32>>, f: &mut Vec<Vec<i32>>, n: usize) -> i32 {
if i >= n || k <= 0 {
return 0;
}
if f[i][k as usize] != 0 {
return f[i][k as usize];
}
let j = search(events, events[i][1], i + 1, n);
let ans = dfs(i + 1, k, events, f, n).max(dfs(j, k - 1, events, f, n) + events[i][2]);
f[i][k as usize] = ans;
ans
}
fn search(events: &Vec<Vec<i32>>, x: i32, lo: usize, n: usize) -> usize {
let mut l = lo;
let mut r = n;
while l < r {
let mid = (l + r) / 2;
if events[mid][0] > x {
r = mid;
} else {
l = mid + 1;
}
}
l
}
dfs(0, k, &events, &mut f, n)
}
}
// Accepted solution for LeetCode #1751: Maximum Number of Events That Can Be Attended II
function maxValue(events: number[][], k: number): number {
events.sort((a, b) => a[0] - b[0]);
const n = events.length;
const f: number[][] = Array.from({ length: n }, () => Array(k + 1).fill(0));
const dfs = (i: number, k: number): number => {
if (i >= n || k <= 0) {
return 0;
}
if (f[i][k] > 0) {
return f[i][k];
}
const ed = events[i][1],
val = events[i][2];
let left = i + 1,
right = n;
while (left < right) {
const mid = (left + right) >> 1;
if (events[mid][0] > ed) {
right = mid;
} else {
left = mid + 1;
}
}
const p = left;
f[i][k] = Math.max(dfs(i + 1, k), dfs(p, k - 1) + val);
return f[i][k];
};
return dfs(0, k);
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.