
SWE, #rust, Competitive Programmer. I am simply the best. β¨Github: https://teapawt.org/t.co/5KKcJ34VEv β¨Codeforces: https://teapawt.org/t.co/uYYkeQXauq
Online
Joined September 2019
- Tweets1.4K
- Following483
- Followers284
- Likes3.3K
Day #100 of #100DaysOfRust, all good things come to an end π§΅
The final boss, the multithreaded web server
βGit: github.com/DreadedHippy/multβ¦
Some time this year, I felt serverely lacking and decided to sharpen my DSA skills.
I stumbled across @fireship_dev 's Rust in 100 seconds
1
19
1,587
Today;s Leetcode: Product Array
Key:
1. Brute force
2. Set a limit on the max allowable product, e.g. Pi(x) for x in [1, 10].
Today's Leetcode: Beware of 0 (I previously solved on Valentine's day last year)
Keys:
1. Generate a prefix product array (PPA) with the numbers
2. The product of last k elements is just the nth product/(n-k)th product where n is length of prefix product array.
3. Notice that...
... with a PPA, all elements after zero are also zero, so if the previous element is 0, we keep the current element alone.
4. Let z be the position of the most recent 0.
5. if the (n-k)-th element comes before z, then we return zero, else see #2
33
Today's leetcode: string search
Keys:
1. Find the first occurence of 'b': f(b)
2. Find the last occurence of 'a': l(a)
3. return l(a) < f(b)
Simpler Alternative:
Check if the string contains "ba"
Today's Leetcode: trees
Keys:
1. Build the adjacency list
2. Depth-first search
3. Keep track of most recent ancestor with given value as you traverse.
4. Get the node's new parent as you traverse
5. let s(x) be subtree size of x, s(x) = 1 + sum(s(y)) for all y which ...
Today's Leetcode
Keys:
1. Sort the array
2. dp[i][0]: max j >= i when nums[i] is unmodified
3. dp[i][1]: modified case
4. Pull DP: dp[i] depends on dp[i+1].
6. It is sometimes optimal to skip an element
7. Simulate skipping by ...
1
10
259
Woah, my solution to today's leetcode has a runtime of 15ms, and the leaderboard starts at 50ms.
I looked into it and it seems my solution is faster than the editorial's π (See image 2, my solution is second)
2
6
469
Today's Leetcode:
Keys:
1. Consider "changing" an element as "deleting" it. The idea behind that can be seen in image 3.
2. It is optimal to delete the 2 lowest numbers, 2 highest numbers, or the highest and the lowest numbers.
3. You can sort or track max and min and calculate
Today's Leetcode:
Keys:
1. Iterate only odd indices
2. Count how many indices i such that s[i] != s[i-1]
8
307
Today's Leetcode: Precomputation + Factorization + DP
Resorted to editorial.π
Keys:
1. Precompute factors of numbers <= n
2. Get minimum cost for all substrings with len >= 2
3. Dynamic programming with memoization for all possible k-splits of the given string.
3
24
Today's Leetcode:
Keys:
1. The problem statement itself is basically the algorithm in plain english
10
374
Today's Leetcode:
Keys:
1. Prefix sums
2. Even and odd prefix sums
3. Even - odd == odd, odd - even == odd
4. Consider the empty prefix, 0.
5. Modulo on your result
Today's Leetcode:
Keys:
1. Prefix sum
2. initialize ans with i32::MAX;
3. check every possible window starting at a given index and greedily minimize ans.
4. Return ans
8
409
Today's Leetcode: Prefix Sum + Monotonic Stack + Binary Search
Keys (to my approach):
1. Keep a prefix sum of the array (for querying)
2. Initialize a monotonic stack (for variable prefix max)
3. Iterate from right to left
4. For current i, get pms(i) defined as ...
1
5
316
... the prefix max sum of the array starting at i e.g.
pms([3,2,1,4,4,]) = sum([3,3,3,4,4])
5. Binary search using your monotonic stack for the j >= i such that pms([i, j]) <= k ans pms([i, j+1]) > k
6. Add the value of (j - i) + 1 to your result.
Return the final result
1
29




























