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

LC 3815 - Design Auction System

Approach This problem is simple, maintain unordered_map for the itemID, for each itermID maintain userId mapping to the bid, and also store this pair inside a unordered_set for fast query. Need to chekc if hte itemId and userId is still inside the hashmap (get 1 WA on this … ) . Code class AuctionSystem { public: unordered_map<int, set<pair<int,int>, greater<pair<int,int>>> > mp; // itemId -> unordered_map<int, unordered_map<int, int> > mp2; // itemId -> AuctionSystem() { mp.clear(); mp2.clear(); } void addBid(int userId, int itemId, int bidAmount) { auto& st = mp[itemId]; auto& st2 = mp2[itemId]; if(st2.count(userId)) { int bid = st2[userId]; st.erase({bid, userId}); st.insert({bidAmount, userId}); st2[userId] = bidAmount; } else { st.insert({bidAmount, userId}); st2[userId] = bidAmount; } } void updateBid(int userId, int itemId, int newAmount) { auto& st = mp[itemId]; auto& st2 = mp2[itemId]; int bid = st2[userId]; st.erase({bid, userId}); st.insert({newAmount, userId}); st2[userId] = newAmount; } void removeBid(int userId, int itemId) { auto& st = mp[itemId]; auto& st2 = mp2[itemId]; int bid = st2[userId]; st.erase({bid, userId}); st2.erase(userId); } int getHighestBidder(int itemId) { return mp.count(itemId) && mp[itemId].size() != 0 ? mp[itemId].begin()->second : -1; } };

January 18, 2026 · 1 min

LC 3814 - Maximum Capacity Within Budget

Approach The problem is to seletc at most 2 datas, and we can seperate this into two problems: select 1 and select 2. For selcet 1, it is really simple, we just get the max capcacity that the cost is smaller then budget. For select 2, we use two pointer to do this in linear time. We sort the data by cost and then try to match from the end. ...

January 18, 2026 · 2 min

LC 3813 - Vowel-Consonant Score

Approach simple problem, calcualte vowel and consonant and do division. Code class Solution { public: int vowelConsonantScore(string s) { int v = 0; int con = 0; for(const char& c: s) { if(c < 'a' || c > 'z') continue; if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ) ++v; else ++con; } return con == 0 ? 0 : v / con; } };

January 18, 2026 · 1 min

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