You are viewing a single comment's thread. Return to all comments →
Hey awesome solution, just a quick question why use -Infinity
You can't set the initial value of 'total' to 0, because the answer might be lower. To avoid that, set the initial value to the lowest possible (-infinity).
The smallest number allowable for any of the numbers in the hourglass is -9. If all the numbers in an hourglass are -9, the lowest possible value for an hourglass is -63. So an initial value of -63 is ok too.
you can also use -64 since the lowest possible value is -63 :)
I think more general and better solution is to declare maxSum outside of loop and then after calculating sum add:
// on first iteration save sum to compare
if (y === 0 && x === 0) maxSum = sum;
This sets maxSum to first calculated sum, whatever it is.