- Intervals - merge intervals, meetings, schedule, max appointments etc.
- Doubly linked List - LRU, Reverse Linked List etc.
- String compressions - occurences of character (e.g. a2b3c1), encode decode string
- Hashmap/Set based problems
- Cyclic Sort for O(n) result in range based array (1,n)
- Recursion - DFS, BFS, Fibonacci, Factorial etc.
- Arrays, Matrix 2D - rotate 90 degree
- Stacks and Queues - paranthesis, monotonic
- Binary Search - order agnostic, rotated, pivot
- Peak element in array
- Pivot element in array
- Order agnostic search
- Find element in rotated array
- 2D array search
- Cyclic Sort - Use it when given range (1,N) - this algo takes O(n) complexity.
- find missing number, duplicate numbers etc.
- For rotation array, its an efficient trick to optimize the rotation count by taking modulo of k by nums.length. Because after every nums.length rotation array is in its original state.
k %= nums.lengthproblem
One input, opposite ends.
l = 0, h = arr.length-1, ans = [];
while(l < h){
if(condition){
l++;
}
h--;
}
return ans;
Two input, exhaust both.
i = 0, j = 0, ans = [];
while(i < arr1.length && j < arr2.length){
if(condition) i++;
else j++;
}
while(i < arr1.length){
i++;
}
while(j < arr2.length){
j++;
}
return ans;
l = 0, ans = [];
for(let r = 0, r < arr.length; r++){
//== add right value to curr
while(condition_broken){
//== remove left value from curr
l++;
}
}
return ans;
preSum = [arr[0]];
for(let i = 1; i < arr.length; i++){
preSum.push(preSum[preSum.length - 1] + arr[i]);
}
return preSum;
l = 0, h = arr.length - 1;
while(l <= h ){
mid = Math.floor((l+h)/2);
if(arr[mid] === target) return mid;
if(arr[mid] > target) r = mid - 1;
else l = mid + 1;
}
return l;
if(root == null) return;
//== logic
dfs(root.left);
dfs(root.right);
return ans;
let stack = [root];
while(stack.length){
const node = stack.pop();
if(node.left) stack.push(node.left);
if(node.right) stack.push(node.right);
}
return ans;
let queue = [root];
while(queue.length){
const len = queue.length;
let nextQ = [];
for(let i = 0; i < len; i++){
const node = queue[i];
if(node.left) nextQ.push(node.left);
if(node.right) nextQ.push(node.right);
}
queue = nextQ;
}
return ans;