2 ms·
For neighbor-sum, instead of saying "each output value is the sum of its input neighbors", we can say "each input value contributes to the output values of its
by ridiculous_fish 8y ago
For neighbor-sum, instead of saying "each output value is the sum of its input neighbors", we can say "each input value contributes to the output values of its neighbors."
Then it is natural to iterate over the input and not the output:
for (int i=1; i < length; i++)
output[i-1] += input[i];
for (int i=0; i + 1 < length; i++)
output[i+1] += input[i];
"Invert the problem" is a really powerful general strategy.
- pure-awesome 8y agoThat's actually the kind of solution I thought the author of the article was going to suggest, before I read their solution. On an unrelated note, why do you use i+1<length rather than i<length-1 ?
- deleted 8y ago[deleted]