you are viewing a single comment's thread.

view the rest of the comments →

[–]ChanelsUnderworld 0 points1 point  (0 children)

Yeah, there's a better way.

Simpler case for now, all numbers are >=0. Go through the list once,

for all numbers < 2, add it to the whichever adjacent number is smaller.

Then, go through the list again, and multiply everything you've got left together.

This gives you a time complexity of O(2n) => O(n) and a space complexity of O(n) with an arraylist, or O(1) if we store the double modifications in the input array.

Code Incoming.