LC 3812 - Minimum Edge Toggles on a Tree

Approach This problem is not really hard, the most complex part is to handle the edge removal. Since for the leaf node, we always need to flip the edge connect to it, so we can do it first, and remove that edge and node from the tree. Iteratively doing this process until can’t flip. The time and space complexity are both O(V+E). Originally I’m using the adjacency list traversal to check if the node is removed, and this is actually a sqaure-time solution, since the node might be like a star-like tree with many edge, and we many need to count multiple times for the node when approaching from different edge. Therefore, I modifed it to track the degree of each node, and this can be done in constant time. ...

January 17, 2026 · 2 min

LC 3811 - Number of Alternating XOR Partitions

Approach I’m thinking that this is a dynamic programming problem, and I use the dfs with memo, but I then find out that the key is idx, maxNum, do1, which is a really large number, and I implemet with a map<pair<int,int>,int> first, but get an TLE. I then change the map<pair<int,int>,int> to unordered_map<long long, int>, getting a pass but still really slow. After reading others’ solution, there is a better way to do this, we maintain the current xor sum cur, and record how many solutions ending with pairing target1 as mp1 (ends with target2 as mp2). When approahcing curent xor sum, if we want it to end with target1, we find if there are solutions that ending with target1 ^ cur, and if there is an anwser, we update mp1(do this also fortarget2andmp2`). ...

January 17, 2026 · 3 min

LC 3810 - Minimum Operations to Reach Target Array

Approach This is really easy problem. For those nums that are the same as target, we don’t need to change it. For the rest, we only need to check how many unique numbers occur in nums, and that is the answer. Code class Solution { public: int minOperations(vector<int>& nums, vector<int>& target) { unordered_set<int> pat; int n = nums.size(); for(int i = 0 ; i < n ; ++i) { if(nums[i] == target[i]) continue; pat.insert(nums[i]); } return pat.size(); } };

January 17, 2026 · 1 min

LC 3809 - Best Reachable Tower

Approach We filter out the towers that is inside the radius, and the second step is to do sorting based on the quality, and x to break-tie, and y to break-tie. An optimization cna be done on the sorting part, since we only have to find the minimum with O(N), no need to do sorting. Code class Solution { public: vector<int> bestTower(vector<vector<int>>& towers, vector<int>& center, int radius) { vector<int> quality; int n = towers.size(); for(int i = 0 ; i < n ; ++i) { int dist = abs(center[0] - towers[i][0]) + abs(center[1] - towers[i][1]); if(dist > radius) continue; // =? quality.push_back(i); } if(quality.empty()) return {-1, -1}; sort(quality.begin(), quality.end(), [&](int& a, int& b){ if(towers[a][2] > towers[b][2]) return true; // quality else if(towers[a][2] < towers[b][2]) return false; if(towers[a][0] < towers[b][0]) return true; // x else if(towers[a][0] > towers[b][0]) return false; if(towers[a][1] < towers[b][1]) return true; // y return false; }); int id = quality[0]; return {towers[id][0], towers[id][1]}; } };

January 17, 2026 · 1 min