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 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 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 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 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 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

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 2976 - Minimum Cost to Convert String I

Approach This is likely a APSP (all pair shortest path) problem, and can be done with Floyd-Warshall algorithm. Originally I was using 4-loops to check if the dist matrix is converging, but actually the algorithm only need 3 loops with k,i,j. I also spend some time to review the floyd warshell algorithm. This is true due to the DP property. the original cost can be formed into a matrix dist_0, and when we construct dist_1, we iterate thorugh k=1 meaning that we allow 1 to be in the shortest path. When doing k=2, we allow 2,1 to be in the shortest path since we combine the solution from dist_1, and is currently looking into solution 2. ...

January 29, 2026 · 2 min

LC 1319 - Number of Operations to Make Network Connected

Approach This is also a union find problem, but I was wrong in the beginning. I though we could merge the first two first, and then keep track of the unconncted nodes, and then check if we have enough resource to connect them one by one. But actually, the unconnected nodes might itself become clustered and we don’t really need to conenct them ony by one. Therefore the correct approach is to merge all of them first, and collect the number of extra edges. Also, we check the number of compoenets comp in the end of merging, and we know that we would need comp-1 more extra edge to merge all the components. ...

January 27, 2026 · 2 min

LC 684 - Redundant Connection

Approach This is a typical union find method, we can union and get parent with amortized O(1) complexity, and when we find the two nodes are already having the same parent, return the answer (this form a loop). Code class Solution { public: struct UnionFind { UnionFind(int n) { parent.assign(n, 0); for(int i = 0 ; i < n ; ++i) parent[i] = i; sz.assign(n, 1); } int GetParent(int a) { if(parent[a] != a) parent[a] = GetParent(parent[a]); return parent[a]; } bool Union(int a, int b) { a = GetParent(a); b = GetParent(b); if(a == b) return false; if(sz[a] < sz[b]) swap(a, b); sz[a] += sz[b]; parent[b] = a; return true; } vector<int> parent; vector<int> sz; }; vector<int> findRedundantConnection(vector<vector<int>>& edges) { UnionFind uf(edges.size() + 1); for(const auto& e: edges) { if(!uf.Union(e[0],e[1])) return {e[0], e[1]}; } return {}; } };

January 27, 2026 · 1 min