week 1 of 15
Hash maps and running variables
Get comfortable with these coding mechanics first: looping with an index, reading and updating a map (get with a default), set membership, building a key from a string or tuple. Template to type from memory by the end of the week: one pass over the array that reads and updates a map or set.
- 1Best Time to Buy and Sell Stock (opens LeetCode in a new tab)Running minimumEasy
- Common mistake
- Starting best at a negative number instead of 0.
- 2Contains Duplicate (opens LeetCode in a new tab)Hash setEasy
- Common mistake
- Adding to the set before checking it.
- 3Valid Anagram (opens LeetCode in a new tab)Frequency countEasy
- Common mistake
- Skipping the length check first.
- 4Two Sum (opens LeetCode in a new tab)Hash map: value to indexEasy
- Common mistake
- Storing x before the lookup lets an element pair with itself.
- 5Maximum Subarray (opens LeetCode in a new tab)Kadane's algorithmMedium
- Common mistake
- Starting best at 0 breaks arrays that are all negative.
- 6Group Anagrams (opens LeetCode in a new tab)Map with a computed keyMedium
- Common mistake
- Using a list as a key. Convert it to a string or tuple.
- 7Top K Frequent Elements (opens LeetCode in a new tab)Frequency map + bucket sortMedium
- Common mistake
- Bucket array of size n instead of n+1.
- 8Longest Consecutive Sequence (opens LeetCode in a new tab)Hash set, find startsMedium
- Common mistake
- Counting from every number makes it O(n^2).
- 9Product of Array Except Self (opens LeetCode in a new tab)Prefix and suffix passesMedium
- Common mistake
- Updating the running product before using it.
- 10Subarray Sum Equals K (opens LeetCode in a new tab)Prefix sum + mapMedium
- Common mistake
- Forgetting the seed, or updating the map before the lookup.