Largest Sum of Non-Adjacent Elements
You are given an array of integers as an argument. Find the greatest sum obtained by adding the integers together. Once integers from the array are added together, you may not add any integers adjacent to those integers.
- Must return a single integer
solve([100,1,50,10,2,18]) > 168
- We pick the largest two numbers 100 and 50 to add and give us 150.
- We cannot use 1 or 10 since they are adjacent to the integers we have already used.
- Our last choices are 2 and 18, so we pick the larger number 18.
- We now cannot use 2 since it is adjacent to the last number we picked, which leaves us with the largest sum of 168.