Reverse Thinking
Maximum Segment Sum After Removals private long[] parents; public long[] maximumSegmentSum(int[] nums, int[] removeQueries) { // reverse union-find int n = nums.length; this.parents =...
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; ...
Fundamentals Line sweep is an algorithm to solve problems like The Skyline Problem. The key idea is to keep track of every change (delta) at each position, then linear scan and process the positio...
Tricks (x >> i) & 1: get the i-th bit in state x (x & y) == x: check if x is a subset of y (x & (x >> 1)) == 0: check if there are no adjancent valid states in x G...
Basic Calculator IV // evaluation map private Map<String, Integer> map = new HashMap<>(); class Term { int coefficient = 1; List<String> variables = new ArrayList<>...
Poor Pigs int poorPigs(int buckets, int minutesToDie, int minutesToTest) { // The number of available states for a pig // e.g. minutesToTest / minutesToDie == 2 // There are 3 availabl...
Guess the Word public void findSecretWord(String[] wordlist, Master master) { int n = wordlist.length; // 10 guesses are allowed at most for (int g = 0, matches = 0; g < 10 &&am...
Fundamentals Infix Prefix (RN) Postfix (RPN) a + b + a b a b + a + b * c + a * b c a b c * + ...
Permutations Permutations of multisets [{n \choose m_{1},m_{2},\ldots ,m_{l}}={\frac {n!}{m_{1}!\,m_{2}!\,\cdots \,m_{l}!}}=\frac {\left(\sum_{i=1}^{l}{m_{i}}\right)!}{\prod_{i=1}^{l}{m_{i}!}}] ...