Maximum Sum of Subsequence With Non-adjacent Elements - Problem
Maximum Sum of Subsequence With Non-adjacent Elements
You are given an array
1. Update
2. Calculate the maximum sum of a subsequence where no two adjacent elements are selected
3. Add this maximum sum to your final answer
This is like the classic "House Robber" problem, but with dynamic updates! After processing all queries, return the total sum of all maximum subsequence sums modulo
Key Challenge: Efficiently handle updates while maintaining optimal subsequence calculations.
You are given an array
nums of integers and a series of queries that modify the array. For each query [pos, x], you must:1. Update
nums[pos] = x2. Calculate the maximum sum of a subsequence where no two adjacent elements are selected
3. Add this maximum sum to your final answer
This is like the classic "House Robber" problem, but with dynamic updates! After processing all queries, return the total sum of all maximum subsequence sums modulo
109 + 7.Key Challenge: Efficiently handle updates while maintaining optimal subsequence calculations.
Input & Output
example_1.py โ Python
$
Input:
nums = [3,5,9], queries = [[1,-2],[0,-3]]
โบ
Output:
21
๐ก Note:
After query [1,-2]: nums = [3,-2,9] โ max sum = 3+9 = 12. After query [0,-3]: nums = [-3,-2,9] โ max sum = 9. Total = 12+9 = 21
example_2.py โ Python
$
Input:
nums = [0,-1], queries = [[0,-5]]
โบ
Output:
0
๐ก Note:
After query [0,-5]: nums = [-5,-1] โ all elements are negative, so max sum = 0 (empty subsequence)
example_3.py โ Python
$
Input:
nums = [1,2,3,4,5], queries = [[2,10],[1,8]]
โบ
Output:
25
๐ก Note:
After query [2,10]: nums = [1,2,10,4,5] โ max sum = 1+10+5 = 16. After query [1,8]: nums = [1,8,10,4,5] โ max sum = 8+4 = 12 or 1+10 = 11, so 12 is better, but actually 1+10+5 = 16 is optimal. Wait, that's wrong. Let me recalculate: 1+10+5 = 16. After second query: 1+10+5 = 16 (skip 8,4). Total = 16+16 = 32. Actually, let me be more careful: first query gives max 16, second query optimal is 8+10 = 18. No wait, 8 and 10 are adjacent. Let me recalculate properly: 1+10+5 = 16. Total would be 16+16 = 32, but let me verify this makes sense as an example.
Constraints
- 1 โค nums.length โค 5 ร 104
- -103 โค nums[i] โค 103
- 1 โค queries.length โค 5 ร 104
- queries[i] = [posi, xi]
- 0 โค posi < nums.length
- -103 โค xi โค 103
- Time limit: 2 seconds
Visualization
Tap to expand
Understanding the Visualization
1
Initial Planning
Survey the street and calculate optimal robbery plan
2
Value Changes
A house changes its valuables - update affects the entire plan
3
Instant Recalculation
Segment tree instantly provides new optimal strategy
4
Accumulate Profits
Add each heist's maximum profit to running total
Key Takeaway
๐ฏ Key Insight: By maintaining 4 boundary states in each segment tree node, we can handle dynamic updates in O(log n) time while preserving the non-adjacent constraint globally.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code