LC 42 - Trapping Rain Water

Approach I was orginally thinking that we can colelct all the peaks, and fill the water between peaks, but finding that this might not be correct. For example like [2,0,1,0,1,0,2], if we fill between peaks, the 3-rd (0-indexed) would be 1, but it is actaully 2. Later I come up with the idea for using mono stack. We collect all the points in increasing order, and try to fill from one to another. We also do the same thing backwards, since the mono-stack would stop on the largest element. ...

January 8, 2026 · 2 min

LC 1339 - Maximum Product of Splitted Binary Tree

Approach I find that any node (except the root), can be split into a differnt subtree, and the remaining nodes are in another subtree. Therefore, all we have to do is to calculate the sum of the subtree, and the overall sum for all nodes, and try to calculate the maximum product. Code class Solution { public: const int mod = 1'000'000'007; int maxProduct(TreeNode* root) { vector<long long> nodeSum; long long sum = traverse(root, nodeSum); nodeSum.pop_back(); long long maxP = LONG_MIN; for(const auto& s: nodeSum) { long long curP = (sum - s) * s; maxP = max(maxP, curP); } return maxP % mod; } long long traverse(TreeNode* root, vector<long long>& nodeSum) { long long sum = 0; if(root->left) sum += traverse(root->left, nodeSum); if(root->right) sum += traverse(root->right, nodeSum); sum += root->val; nodeSum.push_back(sum); return sum; } };

January 7, 2026 · 1 min

LC - Digit DP problems

Approach Usually whne a digit-DP problems occurs it is classify as hard. I’ve read someones answer before, but did not quite understand the strategy. In order to be familiar with digit DP, I try to figure out the systematic method for this, and try to write all of the related problems, including LC-233, 902, 2719, 2827, and I find that actually this type of problem is not that hard. The approach for digit DP is baesd on DP, but with a tight constraint. For instance when traverse to a problem 12xx from 1234, and traverse from 11xx from 1234, the subproblem may be different. And the only difference is the tight part, that is when that digit is approaching the upper limit. ...

January 6, 2026 · 3 min

LC 1611. Maximum Level Sum of a Binary Tree

Approach Todays daily is quite straight forward, just do level search and sum up the value for each level, and see which is the maximum. Code class Solution { public: int maxLevelSum(TreeNode* root) { vector<int> sum; traverse(sum, root, 0); return max_element(sum.begin(), sum.end()) - sum.begin() + 1; } void traverse(vector<int>& sum, TreeNode* root, int level) { if(level >= (int)sum.size()) sum.resize(sum.size() + 1); sum[level] += root->val; if(root->left) traverse(sum, root->left, level+1); if(root->right) traverse(sum, root->right, level+1); } };

January 6, 2026 · 1 min

LC 1975 - Maximum Matrix Sum

Approach This is a math like problem, I think that if a list is given we can always swap the first one to positive and propagate the operation. In the end, there might be no negative number or just a negative number in the end. This depends on the total number of negative numbers in the original case, since the operatoin would not change the odd/even for the total negative count, if original matrix is having odd count, the last element would be negative. ...

January 5, 2026 · 2 min

LC Tips

I spent about two weeks working on the grind 75 peoblems, and would like to record some stuffs. Perhaps There are more stuffs and I’d share in later post. implicit stack space complexity When dealing with a DFS/BFS problem, remember to calculate the implicit time and ‘space’ complexity for the recursive call. Neighbor walking trick I was using somthing like {{0,-1}, {0, 1}, {-1, 0}, {1, 0}} for neighbor grid searching, and find out that there is a method to get the i,i+1 of {0, 1, 0, -1, 0}. This saves a little time for writing. ...

January 5, 2026 · 2 min

LC 1390 - Four Divisors

Approach Originally I thought that we just need to find the number that can be divided by two primes. num = prime1 * prime2, but got an WA, since I’ve ignored the num = prime1 * prime1 * prime1, such as 8 has 1, 2, 4, 8 as divisors. So the overall method is that we precalculate the prime and check the above conditions. My first submit is slow since I tried to build prime up to maxNum, but actually we can build to floor(sqrt(maxNum)). I was thinking that when prime1 is smaller in this square-root range, but prime2 would not, so I build the entire prime inside the whole range. Actually, even when prime2 is larger then the square-root range, we can walk the prime again and check if there are other divisors (this can reduce lots of complexity!). ...

January 4, 2026 · 2 min

LC 3800 - Minimum Cost to Make Two Binary Strings Equal

Approach This seems to be a greedy like problem, and I tried to walk the algorithm by-hand. For a simple case, cross and swap operations can be done by two flips. Therefore, if the cost for cross or swap is larger than two flips, we simply replace it with two flips cost. Then, we check when we need to use swap operation. Let’s say we have two sequence as follows s = 1 .. 0 t = 0 .. 1 If we want to eliminate the first difference by swapping, we need to find another (0, 1) pair to swap, since swapping with (0, 0) and (1, 1) did not change anything, this is just moving the difference backwards. So the greedy method is that we eliminate all the (1, 0) and (0, 1) pairs by swapping. ...

January 4, 2026 · 3 min

LC 3799 - Word Squares II

Approach This is like a backtracking problem, and we find that the size of the words is not large, so this should be the solution. We simply just try all the permutations in words[idx:end] (handling in traverse() function). This is done by swapping idx and i (from idx to end), and then call traverse() recursively. We can swap the idx and i back after the recursive call, in this case we can reuse the words order, and don’t need extra cache for the original words value (this method is commonly used in a backtracking problem). ...

January 4, 2026 · 2 min

LC 3798 - Largest Even Number

Approach We just simply check the last char to see if it is 0,2,4,6,8. We remove the character if it is not one of them, the final string then would be the answer. Code class Solution { public: string largestEven(string s) { while(s.size() > 0) { if((s[s.size()-1] - '0') % 2 == 0) break; s.pop_back(); } return s; } };

January 4, 2026 · 1 min