Skip to content

Make it fast

Read · 1 of 2

Swap the data structure

Most "too slow" fixes don't need a new algorithm, just a better place to keep things: a set instead of a list for "have I seen it?", a dictionary for "how many have I seen?".

Keep the old version as a referee: the fast one must give exactly the same answers.

Read this lesson as text

Same answer, the right data structure.

Swap the data structure

Most "too slow" fixes don't need a new algorithm, just a better place to keep things: a set instead of a list for "have I seen it?", a dictionary for "how many have I seen?".

Keep the old version as a referee: the fast one must give exactly the same answers.

Exercise: Speed it up

This counts pairs of positions whose numbers add up to 0. It's correct, but far too slow for 100,000 numbers. Make it fast and keep the answers the same.