Maximum Non-Adjacent Sum (House Robber) - aloalgo

Maximum Non-Adjacent Sum (House Robber)

Medium

You are given a non-negative integer array nums.

Your task is to find the largest sum of elements from nums such that no two chosen elements are at adjacent indices.

You may assume that: each element nums[i] represents a non-negative value.

Remember that if an element at index i is included in the sum, then elements at indices i-1 and i+1 (if they exist) must not be included.

Return an integer representing the largest possible sum.

Example 1

Input
[5, 5, 5]
Output
10
Explanation:

nums[0] + nums[2] == 10

Example 2

Input
[10, 1, 1, 10]
Output
20
Explanation:

nums[0] + nums[4] == 20

Example 3

Input
[0]
Output
0
Explanation:

nums[0] == 0

Loading...
Input
[5, 5, 5]
Output
10

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!