Leetcode Practice

One leetcode problem a day goes a long way... But even better is two or three a day at medium+ difficulties 🤪

May progress

Dates Completion Notes
5 ✔ - TicTacToe - keep track of scores on each row, col, diagnoal, and anti-diagnoal. There's a winner when the score equals abs(n) in any col, row, diag, or anti-diag.
- Insert interval - Does the new interval come before or after the current interval or does it overlap? If it overlaps, keep updating the new interval itself until we find that the new interval comes before the current interval, or we reached the end of the intervals array.

April progress

Dates Completion Notes
29 ✔ - Lowest common ancestors - LCA is where the split occurs.

March progress

Dates Completion Notes
1 ✔ - Lowest common ancestors - LCA is where the split occurs.
2 ✔ - Validate BST - Update max and min range for left and right subtrees.
3 ✔ - Binary Tree Level Order Traversal - BST using queue. Pop len(queue) children at each level.
- Kth smallest element in BST - Iterative inorder traversal using stack - visit all left nodes first, then pop if curr is null, or add the right node to the stack.
- Construct binary tree from preorder & inorder traversal - Preorder: root -> left -> right. Inorder: left -> root -> right. Given this, you know where the split occurs between the left and right subtrees.
4
5
6 ✔ - Implment trie prefix tree - Create and use a TrieNode with a hashmap to store children of the current letter and mark the end of word.
- Design and add search words data structure - Trie with recursion.

February progress (26 solved in total)

Dates Completion Notes
12 ✔
13 ✔
14 ✔ Longest consecutive sequence - Check if the curr num is the start of a sequence.
15
16
17
18
19
20
21 ✔ - Encode and decode strings - Encode strings by adding the count of chars in each string followed by a symbol to mark it as the end of count and the actual string itself. Decode by iterating through each char in the string until the next symbol mark to determine the count, then splice the string appropriately.
- Valid palindrome - Have l and r pointers starting at both ends of the string. Skip over non-alphanumerical letters. Stop the loop when l > r. Consider an edge case all letters are non-alphanumeric.
22 ✔ - ThreeSum - avoid adding duplicates by skipping if 1) curr indexed pivot is identical to the prev pivot; and 2) nums[l] == nums[l-1].
- Container with most water - greedy problem
- Best time to sell stocks
23
24 ✔ - Longest substring without repeating chars: shift left pointer (adjust window) when a char is repeated.
- Longest repeating character replacement - Window length should be no more than the count of most freq char in the window + k. Shrink the window as needed to meet this condition.
- Valid parenthesis
25
26 ✔ - Find minimum in rotated sorted array
- Search in rotated sorted array
- Merge two sorted lists
- Reverse linked list
- Reorder list - when splitting the list in the middle, make sure the last element in the first linked list points to nothing.
27
28
29 ✔ - Remove nth node from end of list - the right pointer will reach the end of list n steps earlier than the left pointer.
- Linked list cycle - fast pointer will always eventually meet the slow pointer because in each iteration of loop, the distance between them is reduced by 1 (slow pointer moves away from fast pointer by 1, but faster pointer them moves toward it by 2, resulting in the gap between the two reduced by 1. This means time complexity of O(N))
- Invert binary tree - DFS
- Maximum depth of binary tree - the depth of curr node is the max between the left and right children's depth + 1.
- Is same tree - dfs
- Subtree of another tree - Extension of Is Same Tree