LC 3634 - Minimum Removals to Balance Array

Approach We sort the array first, and then find where the k times of the current value will be and update the best solution. I think we can do two pointer, or early termination when the current removal is already larger then best. Code class Solution { public: int minRemoval(vector<int>& nums, int k) { int n = nums.size(); int minRemoval = n; sort(nums.begin(), nums.end()); for(int i = 0 ; i < n ; ++i) { long long maxVal = (long long)k * nums[i]; if(maxVal > INT_MAX) maxVal = INT_MAX; int idx = upper_bound(nums.begin(), nums.end(), maxVal) - nums.begin(); int removal = n - idx; minRemoval = min(minRemoval, removal+i); } return minRemoval; } };

February 6, 2026 · 1 min

LC 3013 - Divide an Array Into Subarrays With Minimum Cost II

Approach Given a solution [ group A ] [i1 .. ] .. [ .. ] [i(k-1)], the distance between i1 and i(k-1)should be smaller then dist, and we assume that now the group A is fixed. The problem now is that we need to findk-2minimum elements in the range[i1 .. i(k-1)](excludingi1` itself), this part is similar to previous daily. Now we tried to do a sliding window, and we the current score is the first element of group A, plus the first element within the sliding window (dist+1 wide), and the sum of k-2 minimum value inside that sliding window (exlude the first one). We maintain the best score and we get the answer after sliding window. ...

February 2, 2026 · 2 min

LC 3010 - Divide an Array Into Subarrays With Minimum Cost I

Approach The answer is find the two minimum from index 1, and add the first one. We sort the nums after index 1, and add the first three. Code class Solution { public: int minimumCost(vector<int>& nums) { sort(nums.begin()+1, nums.end()); return nums[0] + nums[1] + nums[2]; } };

February 1, 2026 · 1 min

LC 3827 - Count Monobit Integers

Approach Simple problem, for all 0, it will be zero, and for all 1, it will be 2^i-1, so we collect all the 2^i-1 within the range. Code class Solution { public: int countMonobit(int n) { int idx = 0; while(true) { int cur = (1 << idx) - 1; if(cur > n) break; ++idx; } return idx; } };

February 1, 2026 · 1 min

LC 3824 - Minimum K to Reduce Array Within Limit

Approach This is a typical search on answer problme, we try to see if the answer is valid, this can be done in simple math by O(N), and the binary search on lgN. Code class Solution { public: int minimumK(vector<int>& nums) { bool isAllNegative = true; int n = nums.size(); for(int i = 0 ; i < n ; ++i) { if(nums[i] > 0) { isAllNegative = false; break; } } if(isAllNegative) return 0; int l = 1; int r = 100000; while(l < r) { int m = l + (r - l) / 2; if(isValid(nums, m)) { r = m; } else { l = m + 1; } } return l; } bool isValid(const vector<int>& v, int k) { int n = v.size(); int cnt = 0; for(int i = 0 ; i < n ; ++i) { if(v[i] <= 0) continue; cnt += v[i] / k; if(v[i] % k != 0) ++cnt; } return (long long)cnt <= (long long)k*k; } };

January 31, 2026 · 1 min

LC 3823 - Reverse Letters Then Special Characters in a String

Approach I store the lowercase and non-lowercase in two stacks, and re-assign each with reverse order by poping from the stack. Code class Solution { public: string reverseByType(string s) { stack<char> stk1; stack<char> stk2; int n = s.size(); for(int i = 0 ; i < n ; ++i) { if(s[i] >= 'a' && s[i] <= 'z') stk1.push(s[i]); else stk2.push(s[i]); } for(int i = 0 ; i < n ; ++i) { if(s[i] >= 'a' && s[i] <= 'z') { s[i] = stk1.top(); stk1.pop(); } else { s[i] = stk2.top(); stk2.pop(); } } return s; } };

January 31, 2026 · 1 min

LC 1092 - Shortest Common Supersequence

Approach This is a dp-like problem, and I was original thinking that for a - b - a - c, we try to match c - a - b as possible. when we match c, we find that it is not possible, when matching a, we would also match the first occurence, since matching a latter one would not be better. Also, we can try to not match the current character, and try to do insertion for this a. This can be recursively done by solvinf problem from the end. ...

January 31, 2026 · 4 min

LC 3534 - Path Existence Queries in a Graph II

Approach This is a really interseting problem that I have not seen before, we can leverage the “binary lifting” to do efficient searching. It is clear that we can sort the nums first, and when two values are reachable, we always walk to the farest node. For instance when we have 2,4,6,8, and max step is 3, we can only jump from 2->4->6->8 with 3 steps. For 2,4,6,8 with step 5, we can jump 2->6 since it is the maximum reachable node. I find that the maximum reachable tree can be construct in O(N) with two pointer teachqniue, but I did not figure out what is the best way to query from the tree. I was thinking that in the worse case, we would need to jump N times, and for q queriues, we would need O(qN). ...

January 31, 2026 · 3 min

LC 2977 - Minimum Cost to Convert String II

Approach This is a really hardr problem, originally I was thinking that for some string like a.. and b.., we must find somethiong to replace the first character, but I did not notice that the problme describe that the changing substring cannot be overlap (it can only be seperate or the same range). I originally think that ace as source and bcd as target, we might have a cost like ce to ed and aed to bcd, then we can combine both to transit to the target. ...

January 30, 2026 · 3 min

LC 3651 - Minimum Cost Path with Teleportations

Approach This is a really hard problem! the idea is easy but there are many case to consider, and also a second part for this problem is to do efficient teleporatoin computation. This is a dp like problme, we can think that when there are no teleport, we can calcualte the answer simply by checking each grid’s top and left. Lets call this answer matrix t0. When doing one teleportation, I was thinking that we can do teleportation from t0, but actually, we can also walk after the teleportation!! Therefore, we need to update t1 first based on t0, and then do the top/left traversal for the t1 again to update the optimal value. ...

January 30, 2026 · 3 min