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 3650 - Minimum Cost Path with Edge Reversals

Approach We re-create the graph with edge weight doubled for reverse direction, and then do BFS-like to find the minimum approaching results, but there are no guarantee for hte time complexity, instead we should apply the dijkstra like approach to get the . Code-1: BFS like (no time complexity guarantee) class Solution { public: int minCost(int n, vector<vector<int>>& edges) { vector<vector<pair<int, int>>> adj(n); for(const auto& e: edges) { adj[e[0]].push_back({e[1], e[2]}); adj[e[1]].push_back({e[0], 2*e[2]}); } vector<int> minCost(n, INT_MAX); queue<pair<int,int>> q; // idx, cost q.push({0,0}); while(!q.empty()) { auto [idx, cost] = q.front(); q.pop(); if(cost >= minCost[idx]) continue; minCost[idx] = cost; for(const auto& [next, w]: adj[idx]) { q.push({next, cost+w}); } } return minCost[n-1] == INT_MAX ? -1 : minCost[n-1]; } }; Code-2: Dijkstra like approach class Solution { public: int minCost(int n, vector<vector<int>>& edges) { vector<vector<pair<int, int>>> adj(n); for(const auto& e: edges) { adj[e[0]].push_back({e[1], e[2]}); adj[e[1]].push_back({e[0], 2*e[2]}); } vector<int> minCost(n, INT_MAX); priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq; // cost, idx pq.push({0, 0}); minCost[0] = 0; while(!pq.empty()) { auto [cost, idx] = pq.top(); pq.pop(); if(minCost[idx] != cost) continue; // outdated if(idx == n - 1) break; for(const auto& [next, w]: adj[idx]) { if(cost + w < minCost[next]) { minCost[next] = cost + w; pq.push({cost+w, next}); } } } return minCost[n-1] == INT_MAX ? -1 : minCost[n-1]; } };

January 27, 2026 · 1 min

LC 450 - Delete Node in a BST

Approach I was originally thinking that we need to find the delete node, and find its parent, and then do some tree operations to reconnect the tree nodes. However, I saw that ususally people tries to copy the successor or predecessors value instead of removing it. In this case the implementation could be more easy. We recursively calling the “deleteNode”. When no child, return nullptr. When one child, remove current node and return that child. For two child, we replace the current node with its successor or predecessor. ...

January 27, 2026 · 1 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

LC 1984 - Minimum Difference Between Highest and Lowest of K Scores

Approach When select k score that have a minimum difference, we would like to make them as close as possible. Therefore, we sort the array and chekc all continuous k elements max difference (that is the i and i+k-1 element’s difference after sorting), ans find the minimum among them. Code class Solution { public: int minimumDifference(vector<int>& nums, int k) { sort(nums.begin(), nums.end()); int n = nums.size(); int minVal = nums[n-1] - nums[0]; for(int i = 0 ; i <= n-k ; ++i) { minVal = min(minVal, nums[i+k-1] - nums[i]); } return minVal; } };

January 25, 2026 · 1 min

LC 3820 - Pythagorean Distance Nodes in a Tree

Approach This is simple questoin, traverse the tree to record the node distance for x, y, and z, and then check if the condition satisfy. Code class Solution { public: int specialNodes(int n, vector<vector<int>>& edges, int x, int y, int z) { vector<int> xs(n, -1); vector<int> ys(n, -1); vector<int> zs(n, -1); vector<vector<int>> adj(n); for(const auto& e: edges) { adj[e[0]].push_back(e[1]); adj[e[1]].push_back(e[0]); } traverse(xs, adj, x, 0); traverse(ys, adj, y, 0); traverse(zs, adj, z, 0); int totalCnt = 0; for(int i = 0 ; i < n ; ++i) { long long a = xs[i]; long long b = ys[i]; long long c = zs[i]; long long m = max(max(a, b), c); if(a*a + b*b + c*c == 2*m*m) ++totalCnt; } return totalCnt; } void traverse(vector<int>& v, const vector<vector<int>>& adj, int root, int level) { if(v[root] != -1) return; v[root] = level; for(const auto& next: adj[root]){ traverse(v, adj, next, level+1); } } };

January 25, 2026 · 1 min

LC 3819 - Rotate Non Negative Elements

Approach We record the non-negative ones’ idx in a separate array and rotate it, and then traverse the original array and mofitied the non-negaitve number based on the rotatete idx array. Code class Solution { public: vector<int> rotateElements(vector<int>& nums, int k) { int n = nums.size(); vector<int> idx; for(int i = 0 ; i < n ; ++i) { if(nums[i] >= 0) idx.push_back(i); } int m = idx.size(); vector<int> idx2(m); for(int i = 0 ; i < m ; ++i) { idx2[i] = idx[(i+k)%m]; } int cnt = 0; vector<int>ans = nums; for(int i = 0 ; i < n ; ++i) { if(ans[i] >= 0) { ans[i] = nums[idx2[cnt]]; ++cnt; } } return ans; } };

January 25, 2026 · 1 min

LC 3818 - Minimum Prefix Removal to Make Array Strictly Increasing

Approach We check the condition from the end and return the number that first breaking the ordering. Code class Solution { public: int minimumPrefixLength(vector<int>& nums) { int n = nums.size(); int minIdx = n-1; for(int i = n-2 ; i >= 0 ; --i) { if(nums[i] >= nums[i+1]) break; minIdx = i; } return minIdx; } };

January 25, 2026 · 1 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 1877 - Minimize Maximum Pair Sum in Array

Approach This seems to be a difficult problem, but it is acutally simple. When we select pairs for a <= b <= c <= d, it is obvious that selecting (a,d) with (b,c) can achieve the minimax; therefore, we only need to sort the original array and find the max with two pointers from head and tali. Code class Solution { public: int minPairSum(vector<int>& nums) { sort(nums.begin(), nums.end()); int sum = INT_MIN; int n = nums.size(); for(int i = 0 ; i < n/2 ; ++i) { sum = max(sum, nums[i] + nums[n-1-i]); } return sum; } };

January 24, 2026 · 1 min