Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>> Exactly: In the first example, they take an array, copy it and add extraneous data for the sole purpose of eliminating a couple if statements?

The IF statements are not even necessary. Compute the sum for the first elements neighbors first (sum[0]=element[1]) and the last element (sum[n-1]=element[n-2]) Then loop over the remaining elements with no conditionals.

I agree with the author that it's probably better to pad it than to have conditionals at every iteration, but for this problem there is no need to have a conditional at all.



Your algorithm breaks for lists of size 0 or 1.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: