- DFS ✅ 2024-10-12
- BFS ✅ 2024-10-12
- Arrays algorithms ✅ 2024-10-26
- Binary trees ✅ 2024-10-26
- Stacks ✅ 2024-10-26
- Dynamic Programming ✅ 2024-11-26
Arrays:
Let’s start with some challenging array-based problems that deal with sorting, searching, and manipulation.
- leetcodearrays Leetcode 238 - Product of Array Except Self: ✅ 2024-10-10
- leetcodearrays Leetcode 41 - First Missing Positive: ✅ 2024-10-10
- leetcodearrays Leetcode 560 - Subarray Sum Equals K: ✅ 2024-10-11
Stacks:
Stacks are great for problems involving balanced parentheses, monotonic stacks, or evaluating expressions.
- leetcodestackLeetcode 20 - Valid Parentheses: ✅ 2024-10-11
- leetcodestack Next Greater Element ✅ 2024-10-11
- leetcodestack 84 Daily Temperatures ✅ 2024-10-11
- leetcodestackEvaluate Reverse Polish Notation ✅ 2024-10-11
- leetcodestack Min Stack ✅ 2024-10-12
- leetcodestack `Remove K Digits ✅ 2024-10-11
- leetcodestack Largest Rectangle in Histogram ✅ 2024-10-12
- leetcodestack Asteroid Collision ✅ 2024-10-12
- leetcodestack Basic Calculator (I, II, III) ✅ 2024-10-12 K digits with stack: when we found a digit, we see if the digit on the left (that is the top of the stack) is larger, so we can pop that digit and replace with the current one, we do as match as K. If at the end of the loop we still have count < k, we pop
Binary Search:
Binary search isn’t just limited to searching in sorted arrays. It can be used in various forms to solve interval and range-related problems.
- leetcodebinarysearchLeetcode 33 - Search in Rotated Sorted Array: ✅ 2024-10-13
- leetcodebinarysearch Leetcode 81 - Search in Rotated Sorted Array II: ✅ 2024-10-28
- leetcodebinarysearch Leetcode 287 - Find the Duplicate Number: ✅ 2024-10-28
Binary Trees:
Binary trees will build on your traversal skills while introducing problems around paths, depth, and tree properties.
-
Leetcode 105 - Construct Binary Tree from Preorder and Inorder Traversal:
-
Leetcode 114 - Flatten Binary Tree to Linked List:
-
Leetcode 543 - Diameter of Binary Tree:
| Exercise | Level | Strategy | Solution |
|---|---|---|---|
| Leetcode 238 - Product of Array except self | Medium | Prefix and suffix array | Multiply prefix and suffix array except i |
| Leetcode 241 - First missing positive | Hard | Reordering | Put val in index val-1 in the array. Traverse array, first unmatched index is solution |
| Leetcode 560 - Subarray sum equals K | Medium | Hashtable + prefix array | Traverse the array and put current prefix sum in the hash. If hash contains prefix sum - k, that’s a solution |
| Leetcode 34 Find first and last index in a sorted array | Medium | Binary search | Find the element via binary search and expand |
| Leetcode 33 Search in rotated array | Medium | Binary search | Check if the first half if sorted, if the element is in the first half range, update right, else left else if the second half is sorted, check if the element is in range and update r, else update l |
| Leetcode 81 Search in rotated array II | Medium | Binary Search | The duplication makes possible to have situation where you have nums[l] nums[mid] nums[r] and impossible to distinguish. Since none of them is the solution, move l by 1 to restrict the scan |
| Leetcode 852 Peak Index in a Mountain Array | Medium | Binary Search | Check if arr[mid] > arr[mid+1], if it is, go left, because you are on downard slope on the right, else go right |
| Leetcode 153. Find minimum in Rotated Sorted Array | Medium | Binary Search | After sorting [4,5,6,7,1,2,3] the array will be split in a larger left part [4,5,6,7] and a smaller right part [1,2,3]. If the right part is sorted, then the minimum is either in the left part or at mid (rotation might occur just before). If the right part is unsorted, the minimum is there |
| Leetcode 658 Find K closest element in Binary search | Medium | Binary Search | Find the leftmost location of the window by restrict binary search [0, len(arr)-k]. Return arr[l:l+k]. Compare array[mid]-x with array[mid+k] - x |
| Leetcode 875 Koko Eating Bananas | Medium | Binary Search | Set speed min 1, max = max(pile), use binary search and exit if time exceed hours |