House Robber
Problem statement
Houses stand in a row along a street. nums[i] is the amount of money in house i.
You may take the money from any houses you like, but you can never take from two houses that are next to each other. Houses i and i + 1 are next to each other. The street does not wrap around, so the first and last houses are not neighbors unless there are only two houses.
Return the largest total amount of money you can take.
Function
rob(nums: int[]) → intExamples
Example 1
nums = [1,2,3,1]return = 4Take houses 0 and 2 for 1 + 3 = 4. Taking houses 1 and 3 gives only 2 + 1 = 3.
Example 2
nums = [2,7,9,3,1]return = 12Take houses 0, 2, and 4 for 2 + 9 + 1 = 12. No two of them are next to each other.
Example 3
nums = [2,1,1,2]return = 4Take houses 0 and 3 for 2 + 2 = 4. The best choice does not have to be every other house: houses 0 and 2 give only 3.
Constraints
1 <= nums.length <= 10^40 <= nums[i] <= 10^4