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 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 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 3796 - Find Maximum Value in a Constrained Sequence

Approach This seems like there is a greedy approach. I think that we can represent by a range. If index i is constraint to be smaller than 2, its range is [0, 2]. And if its difference to the next point is smaller than 3, the range for next point is [-3, 5], and we can keep propagate to the right side. Later I found that the number would be positive, so actually we don’t need to care about the negative part, just calculate the largest part. ...

January 3, 2026 · 2 min