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

LC 3510 - Minimum Pair Removal to Sort Array II

Approach This is a raelly interesting problem. same problem with yesterdays daily but need to come up with a much faster method. I was originally thinking that checking a vector to be sorted is O(N), and we can only done in O(N^2), and after reading one solution, I find that we only need to check the neigobers order, and count the number of ordered pairs. In this case, we can check O(1) to update the number of ordered pairs, and this can be done in O(1). ...

January 23, 2026 · 3 min

LC 3314 - Construct the Minimum Bitwise Array I

Approach I found that the answer for this is find the longest continuous 1’s in the number, and remove that 1. For instance, 1000111 -> 1000011. I also found an interesting thing to do this without using while loop to shift, the formula for this is n & ~((~n & (n+1)) >> 1). The part (~n & (n+1)) >> 1 is the highest continuos 1 bit since for n = ? 0 1 .. 1, we have n+1 = ? 1 0 .. 0, then ~n & (n+1) = 0 1 0 .. 0, shifting this by one is the highest bit. ...

January 20, 2026 · 1 min

LC 1292 - Maximum Side Length of a Square with Sum Less than or Equal to Threshold

Approach I think that we can do a O(N^3) approach by maintaining prefix sum, and then do post processing to get the currnet sume for all pairs of square from i,j to i+k,i+k. My first version was slow where I use k=1 to end, it can be much faster by changing it to k=maxWith+1 to end; however, the worst case comeplxity is still large. An interseting part is the 2D prefix, it can be done easily by adding in the horizontal direction then the vertical direction. ...

January 19, 2026 · 3 min