Mathematics
Theorem Lagrange’s four-square theorem Lagrange’s four-square theorem, also known as Bachet’s conjecture, states that every natural number can be represented as the sum of four integer squares. T...
Theorem Lagrange’s four-square theorem Lagrange’s four-square theorem, also known as Bachet’s conjecture, states that every natural number can be represented as the sum of four integer squares. T...
Greatest Common Divisor Euclidean algorithm public int gcd(int a, int b) { while (b != 0) { int tmp = b; b = a % b; a = tmp; } return a; } Variants Greatest...
Subtree of Another Tree Binary Lifting This is a good tutorial on Binary Lifting. Kth Ancestor of a Tree Node // dp[i][j] = j-th node's (2 ^ i)-th ancestor in the path private int[][] dp; priva...
Selection algorithm Heap Sort Kth Largest Element in an Array public int findKthLargest(int[] nums, int k) { // min heap Queue<Integer> pq = new PriorityQueue<>(); for (i...
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 ...