Dynamic Programming (Rolling)
Check if There is a Valid Partition For The Array public boolean validPartition(int[] nums) { boolean[] dp = {true, false, nums[0] == nums[1], false}; int n = nums.length; for (int i =...
Check if There is a Valid Partition For The Array public boolean validPartition(int[] nums) { boolean[] dp = {true, false, nums[0] == nums[1], false}; int n = nums.length; for (int i =...
Minimum Amount of Time to Fill Cups public int fillCups(int[] amount) { int max = 0, sum = 0; for (int a : amount) { max = Math.max(max, a); sum += a; } // 1 cup of...
Fundamentals Segment tree Efficient range query, while array modification is flexible. Implementation Recursive The standard (recursive, top-down) Segment Tree requires \(4n\) vertices for wor...
Iteration Length of the Longest Valid Substring private static final int MAX_FORBIDDEN_LEN = 10; public int longestValidSubstring(String word, List<String> forbidden) { Set<String&g...
Divide and conquer + Memoization. Maximum Value of K Coins From Piles private Integer[][] memo; public int maxValueOfCoins(List<List<Integer>> piles, int k) { this.memo = new Int...
Majority element: an element that occurs repeatedly for more than half of the elements of the input. Boyer-Moore Voting Algorithm Boyer–Moore majority vote algorithm: finds the majority of a sequ...
The dynamic programming problems are not self-contained, i.e. external information are required. Burst Balloons public int maxCoins(int[] nums) { int n = nums.length; // dp[i][j]: max co...
Maximum Segment Sum After Removals private long[] parents; public long[] maximumSegmentSum(int[] nums, int[] removeQueries) { // reverse union-find int n = nums.length; this.parents =...
Reverse Substrings Between Each Pair of Parentheses public String reverseParentheses(String s) { Deque<Integer> st = new ArrayDeque<>(); int[] pairs = new int[s.length()]; ...
Count Numbers At Most N Given Digit Set public int atMostNGivenDigitSet(String[] digits, int n) { String s = Integer.toString(n); int nLength = s.length(), m = digits.length, count = 0; ...