Maximize sum of non-adjacent values
SamsungMachine Learning Engineer · OnsiteLast updated
This question evaluates algorithmic problem-solving skills, specifically understanding of dynamic programming and optimization when selecting non-adjacent elements, within the Coding & Algorithms domain.
Problem
You are given an integer array nums representing the amount of money available at each house along a street.
If you take money from house i, you cannot take money from house i-1 or i+1 (no two selected houses may be adjacent).
Return the maximum amount of money you can take.
Input
nums: an array of integers (typically non-negative)
Output
- An integer: the maximum sum achievable without choosing adjacent elements.
Constraints
1 <= nums.length <= 10^50 <= nums[i] <= 10^9
Examples
- Input:
nums = [1, 2, 3, 1]→ Output:4(choose 1 and 3) - Input:
nums = [2, 7, 9, 3, 1]→ Output:12(choose 2, 9, 1)
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.