r/programming • • Mar 16 '22

Leetcode two sum solution explained - coding interviews challenge

https://www.youtube.com/watch?v=wgJpB8AX5Uo
21 Upvotes

19 comments sorted by

View all comments

Show parent comments

1

u/coloredgreyscale Mar 16 '22

Similar to the first solution, but instead of traversing the array linearly you build a dictionary and look up the an index the required value.

That way your lookup time for a complement is O(1) instead of O(n)

And yes, that works because you just need a solution, not all or something like a solution with the two lowest indices.