LC 85 - Maximal Rectangle

Approach Today’s daily can be solved with the technique as mono-stack in LC-84. First, we calculate the continuous 1 in the horizontal direction (only need to calculate one side). For instance, given 0 1 1 0 0 1 1 1 we have 0 1 2 0 0 1 2 3 denoting how many ones are there, and then we process each vertical direction as it is a histogram (as LC-84). ...

January 11, 2026 · 2 min

LC 1235 - Maximum Profit in Job Scheduling

Approach this is a dp like problem, we maintain for each endTime what is the best profit, then for the next subproblem, we just combine the sub-solution for taking current interval or not. If we take hte current interval, we use the upper_bound to get the best cost that end before the starting time of current interval, otherwise we take the privious solution. One part that we need to be careful with is the duplicated endTime, we need to maintain the max if there are multiple intervals with the same endTime. ...

January 10, 2026 · 2 min

LC 712 - Minimum ASCII Delete Sum for Two Strings

Approach Todays daily is a dp-like problem, and we maintain a n+1 times m+1 memo, and see if we want to match the current character, if not we remove either from s1 or s2 and proceed the dp. This is correct but we can actually be faster using a bottom-up approach, so I rewrite the idea with bottom up approach. After consulting with AI, the reason for faster in bottom-up mainly due to (1) lower L1/L2 cahce hit due to better locality, (2) no need of function stack, (3) slightly lower branching, but there are some problems that are not favorable for using bottom up aproach such as digit-dp, tree-dp, graph-dp. ...

January 10, 2026 · 2 min

LC 84 - Largest Rectangle in Histogram

Approach For each value in the vector, we need to find the first one that is smaller than it in the left (and right), and this can be done with mono-stack. So we maintain a mono-stack and record the first index smaller than it on the left (and right) hand side, and in the end right-left-1 times the current value is the maximum profit for this position. Code class Solution { public: int largestRectangleArea(vector<int>& heights) { heights.insert(heights.begin(), INT_MIN); heights.insert(heights.end(), INT_MIN); int n = heights.size(); vector<int> left(n, -1); vector<int> right(n, -1); { stack<int> stk; stk.push(0); for(int i = 1 ; i < n-1 ; ++i) { while(!stk.empty()) { int prevIdx = stk.top(); if(heights[prevIdx] >= heights[i]) { stk.pop(); } else { left[i] = prevIdx; stk.push(i); break; } } } } { stack<int> stk; stk.push(n-1); for(int i = n-2 ; i >= 0 ; --i) { while(!stk.empty()) { int prevIdx = stk.top(); if(heights[prevIdx] >= heights[i]) { stk.pop(); } else { right[i] = prevIdx; stk.push(i); break; } } } } // calculate int prof = INT_MIN; for(int i = 1 ; i < n-1 ; ++i) { prof = max(prof, (right[i]-left[i]-1)*heights[i]); } return prof; } };

January 10, 2026 · 1 min

LC 127 - Word Ladder

Approach I was thinking a naive approach to build the graph for neighbor string first, and then try to do BFS. so the adjacency list part is TC O(MN^2), and the BFS part is O(M). I saw a different method and think that it has lower complexity (actually not). We put the beginning number into the queue for BFS, and its neighbor is having at most O(25M) neighbors, since we can change one character among M to any other 25 characters, and we only need to hash the wordList and find if it is in the hash. If it is present in the hash we erase it and put it into the queue. In this case, all the element ahve at most 25M neighbor, and number of traversal is at most N, but the complexity is still O(M^2N) since hashing a length M string is O(M). ...

January 9, 2026 · 3 min

LC 224 - Basic Calculator

Approach This problem is not hard but need to consider some special conditions. I found that actually we just need to trace the actual sign after removing the parantheses. For instance, 1-(2-(3+4)) is equal to 1-2+3+4, so we need to buffer the current actual sign when we get the (. In this case, we stack - when getting the first (, and when approaching the second (, we know that it is negative outside, and this is negative so we need to invert the sign inside this parantheses, so stack + inside. And the sign is removed after ) is encountered. ...

January 9, 2026 · 2 min

LC 23 - Merge k Sorted Lists

Approach This is actually quite simple, just take the smallest from all list and connect, and iteratively do this process, and will use a min heap instead for faster min-val query. I also tried a new method for defining the comparator for priority_queue this time. Usually I will use a comparator class and a bool operator(), but I found that there is a more clear way to write this, that is to define the lambda function first, and pass the function to pq’s constructor. ...

January 9, 2026 · 1 min

LC 295 - Find Median from Data Stream

Approach I waw originally thinking that we can insert the number with binary search (or lower_bound), and find median with directly taking the number from index for the vector. This strategy can pass the porlbme, but this is actually an O(N) solution since vector insert can be O(n). I then find out that we can maintain a minheap and maxheap with O(lgN) insertion time, and we only need to rebalance two heaps when their size is different by more than 2. ...

January 9, 2026 · 2 min

LC 865 - Smallest Subtree with all the Deepest Nodes

Approach My idea is to sweep the tree and collect the maxDeep and maxCnt, and then do the second pass to track the count for maxDeep, and return the first node that has cnt reaching maxCnt`. Code in code-1. After this I ask AI to have an optimized version, and it give me code-2, where we can directly store the depest node, and its ancestors. when left and right subtree have the same deep, we can return the current subtree and the deep+1, else we return the best solution among two subtree. ...

January 9, 2026 · 2 min

LC 1458 - Max Dot Product of Two Subsequences

Approach Today’s daily is quite strait forward, the optimal structure is presented so we can leverage DP. For maxProd([idx1 ..], [idx2 ..]), we can split into subproblems: Take idx1 & idx2 for SP, and don’t do SP for the rest -> nums[idx1] * nums[idx2] Take idx1 & idx2 for SP, and try to add with next solution -> nums[idx1] * nums[idx2] + maxProd([idx1+1 ..], [idx2+1 ..]) Let idx1 pair with others -> maxProd([idx1 ..], [idx2+1 ..]) Let idx2 pair with others -> maxProd([idx1+1 ..], [idx2 ..]) Notice that for situation 2, 3, 4, the need to make sure that at least one element is paired (we cannot have empty pairs) Code class Solution { public: int maxDotProduct(vector<int>& nums1, vector<int>& nums2) { int m = nums1.size(); int n = nums2.size(); memo.assign(m, vector<int>(n, -1)); return traverse(nums1, nums2, 0, 0); } int traverse(const vector<int>& nums1, const vector<int>& nums2, int idx1, int idx2) { if(idx1 == nums1.size() || idx2 == nums2.size()) return INT_MIN; if(memo[idx1][idx2] != -1) return memo[idx1][idx2]; int maxSP = INT_MIN; maxSP = max(nums1[idx1]*nums2[idx2], maxSP); int nextSP = traverse(nums1, nums2, idx1+1, idx2+1); if(nextSP != INT_MIN) maxSP = max(nums1[idx1]*nums2[idx2] + nextSP, maxSP); nextSP = traverse(nums1, nums2, idx1, idx2+1); if(nextSP != INT_MIN) maxSP = max(maxSP, nextSP); nextSP = traverse(nums1, nums2, idx1+1, idx2); if(nextSP != INT_MIN) maxSP = max(maxSP, nextSP); memo[idx1][idx2] = maxSP; return maxSP; } vector<vector<int>> memo; };

January 8, 2026 · 2 min