You are given an integer array nums (values may be negative). Walk through it from left to right and, for each element, either take it (adding it to your total) or skip it, but you may never skip two consecutive elements. Taking every element is allowed, and the very first element may be skipped.
Return the maximum total you can collect.
Examples
Example 1
Input: nums = [3,-1,4]
Output: 7
Explanation: Take 3, skip -1, take 4.
Example 2
Input: nums = [-5,-6,-7]
Output: -6
Explanation: Skip -5, take -6, skip -7. Skipping both -5 and -7 is fine because they are not consecutive. Any other pattern takes at least two elements.
Example 3
Input: nums = [-2,5,-1,-1,3]
Output: 7
Explanation: Skip -2, take 5, skip the first -1, then the second -1 must be taken, take 3: 5 - 1 + 3 = 7.
Constraints
1≤nums.length≤105
−109≤nums[i]≤109
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up