Number Theory Note: inverse of a number (mod x)
Assume you’re calculating a great number. However, the number is very large and you’re required to return the number mod $10^9+7$. This is really common in algorithm problems and $10^9+7$ is a larg...
Assume you’re calculating a great number. However, the number is very large and you’re required to return the number mod $10^9+7$. This is really common in algorithm problems and $10^9+7$ is a larg...
bisect FUNCTIONS import bisect testArray = [1,2,3,3,3,5,5,6,7,8] # 1 2 print(bisect.bisect_left(testArray, 2), bisect.bisect_right(testArray, 2)) # 2 5 print(bisect.bisect_left(testArray, 3), bis...
LeetCode 3850. Count Sequences to K Intuition The subproblem is, how many steps start from $DP[i,c_2,c_3,c_5]$ to $DP[i+1,c_2,c_3,c_5]$, then find $DP[n, t_2,t_3,t_5]$ This sounds difficult to fi...
LeetCode 1653. Minimum Deletions to Make String Balanced Intuition What is a balanced string? In this problem, for every $i < j$ there is no $s[i] = b$ and $s[j] = a$. We can conclude: the bal...
stratascratch 2054 Consecutive Days Intuition & Approach The question requires to write a sql query to find any user who has 3 consecutive days with activity. Step 1: Simple Intutive Solutio...
LeetCode 3830 Longest Alternating Subarray After Removing At Most One Element Intuition & Approach Step 1: Subproblem What is the purpose of this problem? increase, decrease, increase, decre...
Continuous Distributions Basic Definitions PDF ($f(x)$): \(P(X \in B) = \int_B f(x)dx, \quad P(a \le X \le b) = \int_a^b f(x)dx\) Properties: $f(x) \ge 0$ and $\int_{-\infty}^{\infty} f(x)dx = 1$...
Leetcode 2435. Paths in Matrix Whose Sum Is Divisible by K Intuition Step1: subproblem. This can be 3D DP: $DP[i][j][k]$ indicate the #(routes) from $[0,0]$ to $[i,j]$ with value k. Then the goa...
Leetcode contest I took Leetcode contest yesterday, Biweekly 168 and Weekly 473. I solve 3 in Biweekly and 2 in Weekly, and I get TLE/MLE on rest questions. At least, I got bad idea, it’s much b...
I will give all examples in ascending order, just change the judgement clause when you need descending order. $O(n^2)$ sorting algorithms Selection Sort The idea is, always select the smallest v...