Tree Traversal
Traversal Preorder Binary Tree Preorder Traveral Recursion public List<Integer> preorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<>(); helper(root, li...
Traversal Preorder Binary Tree Preorder Traveral Recursion public List<Integer> preorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<>(); helper(root, li...
Fundamentals The most commonly way of DFS is to call a helper function recursively. Due to the nature of recursion, we can instead use a Stack. Keys and Rooms Recursion public boolean canVisitA...
Sort an Array Quicksort Quicksort: not stable Lomuto Partition Scheme public int[] sortArray(int[] nums) { quickSort(nums, 0, nums.length - 1); return nums; } private void quickSor...
Iteration while iterating through an array, keep a running state to some variables. Maximum Value of an Ordered Triplet II long long maximumTripletValue(vector<int>& nums) { long l...
Definition a[i_0], a[i_1], ..., a[i_k] Where 0 <= i_0 < i_1 < ... < i_k <= a.length Algorithm Greedy Shortest Impossible Sequence of Rolls public int shortestSequence(int[] roll...
Definition a[i], a[i + 1], ..., a[j] Where 0 <= i <= j <= a.length Algorithm Minimum Moves to Make Array Complementary public int minMoves(int[] nums, int limit) { // delta array ...
Distant Barcodes Let n = barcodes.length. Find the elements barcodes[i] with the most occurrences \(o_{max}\). Assume an element barcodes[k] has occurences \(o_k\). Steps: Put barcodes[i] to t...
Semaphore Semaphore Conceptually, a semaphore maintains a set of permits. Semaphore(int): a bowl of marbles acquire(): takes one marble from the bowl; waits if there are none release(): ad...
Edit Distance public int minDistance(String word1, String word2) { int n1 = word1.length(), n2 = word2.length(); // dp[i][j]: word1.substring(0, i) -> word2.substring(0, j) int[][]...
Underscores in Numeric Literals In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to...