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