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

HLS & LLVM

Last few days I learned that the C++ translation to IR requires AST (abstract syntax tree), and it seems to me that this is much the same as the HLS. From HLS (high leve synthesis) tool, you input a C++ code and output a RTL, usualy verilog code, which is a hardware descriptoin language. I learned HLS and used tools before, for FPGA mapping we use Vivado HLS, or called Vitis now (original owned by Xilinx, which is aquired by AMD). I think if we can leverage the same technique, perhaps we can do the same techqiue and map arbitary C++ code to RTL. ...

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

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

Why This Blog?

I like to study different ideas, and often ask AI a field that I’m not quite understand, and I find that I can learn a lot during the discussion. Also, I think that writing it out is a great way to review the idea, and also making some records.

January 3, 2026 · 1 min