↓Skip to main content

Math

LeetCode 1627: Graph Connectivity With Threshold
·2 mins
LeetCode Hard Math Number-Theory Union-Find Array
LeetCode 1627 note: no pairwise GCDs; for each z > threshold, connect all multiples of z, O(n log n) unions in total
LeetCode 3895: Count Digit Appearances
·1 min
LeetCode Medium Math Array
LeetCode 3895 note: convert each number to a string and add up how many times the digit appears
LeetCode 2912: Number of Ways to Reach Destination in the Grid
·3 mins
LeetCode Hard Dynamic-Programming State-Machine Math Combinatorics
LeetCode 2912 note: many cells, but only four classes relative to the destination (itself, same row, same column, neither); each class’s total moves together in O(k)
LeetCode 60: Permutation Sequence
·2 mins
LeetCode Hard Math Combinatorics Recursion
LeetCode 60 note: make k 0-indexed; once the first digit is fixed there are (n-1)! arrangements, so k // (n-1)! picks the first digit among those remaining, then repeat for the next position
LeetCode 902: Numbers At Most N Given Digit Set
·2 mins
LeetCode Hard Dynamic-Programming Digit-Dp Math Binary-Search String
LeetCode 902 note: digit DP with state (pos, tight, started); before starting, a position can be skipped, representing numbers with fewer digits than n
LeetCode 3984: Divisible Game
·3 mins
LeetCode Medium Dynamic-Programming Kadane Math Number-Theory Enumeration
LeetCode 3984 note: replacing a composite k by one of its prime factors only helps (more values divisible, smaller k), so try every prime factor of the numbers plus 2, running Kadane once per candidate
LeetCode 3959: Check Good Integer
·1 min
LeetCode Easy Math Simulation
LeetCode 3959 note: accumulate the square sum minus the digit sum; each digit adds d² − d ≥ 0, so the gap never shrinks and you can return as soon as it reaches 50
LeetCode 3932: Count K-th Roots in a Range
·3 mins
LeetCode Medium Binary-Search Math
LeetCode 3932 note: the answer is floor(r^(1/k)) - floor((l-1)^(1/k)); take roots with integer binary search and count 0 too
LeetCode 3918: Sum of Primes Between Number and Its Reverse
·2 mins
LeetCode Medium Math Number-Theory
LeetCode 3918 note: reverse n, take the range [min, max], and sum primes by trial division; test divisibility with %, never by comparing // with /
LeetCode 3908: Valid Digit Number
·1 min
LeetCode Easy Math
LeetCode 3908 note: convert to a string and check that it contains x but does not start with x
LeetCode 3899: Angles of a Triangle
·2 mins
LeetCode Medium Math Geometry
LeetCode 3899 note: check the triangle inequality first, compute the three angles with the law of cosines, and remember to sort them
LeetCode 3828: Final Element After Subarray Deletions
·2 mins
LeetCode Medium Math Brainteaser Game-Theory Array
LeetCode 3828 note: Alice can leave only the first or last element on her first move, guaranteeing max(first, last); Bob can likewise keep only an end, and one deletion changes at most one end, so the answer is max(first, last)