← Snowflake Interview Insights
Sort events by end day, then use dynamic programming where dp[i][k] represents the maximum value using the first i events and attending at most k events. For each event, either skip it or attend it and combine with the best previous non-overlapping event (found via binary search) using one fewer event. The answer is dp[n][K].
Pro tip: Clarify the boundary condition: 'two events cannot share a boundary day' means if one event ends on day d, the next must start on day d+2 or later. Also, mention that if K is large enough, the problem reduces to weighted interval scheduling without the cardinality constraint.
Restate the problem to confirm understanding: events have start, end, value; attend at most K events; no overlapping and no shared boundary days. Ask about constraints (e.g., number of events, value ranges) to guide algorithm choice.
Sort events by end day. For each event, compute the index of the latest non-conflicting event using binary search on end days, ensuring the next event starts after the current event's end day plus one (i.e., start > end + 1).
Let dp[i][k] be the max value using first i events with at most k events attended. Recurrence: dp[i][k] = max(dp[i-1][k], value[i] + dp[p(i)][k-1]) where p(i) is the latest non-conflicting event index.
Iterate k from 1 to K and i from 1 to n, filling the DP table. Optimize space by using two 1D arrays (previous and current) since dp[i][k] depends only on dp[i-1][k] and dp[p(i)][k-1].
Return dp[n][K]. Time complexity O(n log n + nK) due to sorting, binary search, and DP. Space complexity O(n) with optimization. Discuss potential improvements if K is large.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.