This question evaluates a candidate's understanding of array-based optimization and dynamic programming concepts for computing a maximum sum under non-adjacent selection constraints.
You are given an integer array nums where nums[i] is the value in position i.
Select a subset of indices such that no two selected indices are adjacent (i.e., you cannot select both i and i+1). Return the maximum possible sum of the selected values.
nums
: array of integers (can include
0
)
1 <= nums.length <= 2 * 10^5
0 <= nums[i] <= 10^9
O(n)
O(1)
or
O(n)
nums = [2, 7, 9, 3, 1]
→
12
(pick
2 + 9 + 1
)
nums = [1, 2, 3, 1]
→
4
(pick
1 + 3
)