4 ms·
sum += (x > 0 && y > 0 && x < x_limit && y < y_limit) ? a[y][x] : 0; That took less than a minute of thought; or you could use a common technique in image and
by userbinator 1y ago
sum += (x > 0 && y > 0 && x < x_limit && y < y_limit) ? a[y][x] : 0;
That took less than a minute of thought; or you could use a common technique in image and video processing, which is to make sure that the edges are always present but filled with appropriate values by defining your grid to be slightly bigger than the active area.
- what 1y agoAnd it doesn’t do what’s asked? Congrats.
- userbinator 1y agoI just concentrated on what would be the hardest part, the "edge" cases (literally.) Wrapping that in a loop that goes through the appropriate indices is not much more work. There is a sibling dead comment to mine which has the rest.
- peterfirefly 1y agoShould probably use >=
- userbinator 1y agoYou're assuming closed instead of open intervals.
- peterfirefly 1y agoI was assuming zero-based arrays.
- jay_kyburz 1y agooops doesn't count the edges. Take another minute. -- joking -- :)