Young Devs Bin
๐Ÿ“ Today I Learned

Two Sum โ†’ Two Pointer โ†’ Three/Four Sum

ยท1 min readยท#leetcode#algorithm#two-pointer
  • Two Sum with HashMap is O(n) time, O(n) space
  • Two Pointer needs sorted array, but uses O(1) space โ€” good answer when interviewer asks "can you do better on space?"
  • Three Sum โ†’ fix i, use two pointers for the rest
  • Four Sum โ†’ fix i and j, use two pointers for the rest
  • Same pattern, just more anchor points

Full breakdown here.