hallock
BAN USERJavascript:
ns => {
var maxDiff = -1;
var curMax = -1;
var i;
for(i=0; i < ns.length; ++i) {
if (curMax < ns[i]) {
curMax = ns[i];
continue;
}
maxDiff = Math.max(maxDiff, curMax - ns[i]);
}
return maxDiff;
}
functional solution:
ns => ns.reduce((m, n) => {
return {
curMax: Math.max(n, m.curMax),
maxDiff: Math.max(m.maxDiff, m.curMax - n)
}
}, {curMax: -1, maxDiff: -1}).maxDiff;
@Shilpi, "given that second element comes after the first one." means that the lower number in the diff has to come after the higher number. In 1,2,100, there is no lower number to create a diff, so zortlord's algo returns 0. (Although it should return -1 since there are no drops at all.)
- hallock August 17, 2015
I think you meant "subset_sum" instead of "foo"
- hallock August 17, 2015