Given an input array of integers, pick some numbers from the beginning of the array and some numbers from the end of the array in such a way that their sum is minimized. Return this minimum sum.
Note: You are allowed to pick 0 numbers, in that case return 0. Or you can pick all numbers.
Example
Input: nums = [-2, 5, 2, -1, 3, -10, 9, -2]
Output: -5
Explanation: You can pick the first and the last 3 numbers
(-2) + (-10) + 9 + (-2) = -5
Or you can remove the first 6 and the last one
(-2) + 5 + 2 + (-1) + 3 + (-10) + (-2) = -5
Walkthroughs of the solution. Try the steps first, then watch to compare approaches.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check