LC 3850 - Count Sequences to K

To check whether it can become an integer k, we track prime-exponent vectors instead of doing fraction arithmetic. In this problem we only care about primes {2,3,5} since the number is 1 to 6. If k contains any other prime factor, it is impossible, return 0. Then we do the search backwards, process nums from right to left, and at each position, do unchanged, multiple or divide. When we finish all indices we add the total count by one if the remaining target is 1. ...

February 22, 2026 · 3 min

LC 91 - Decode Ways

Match the currnet idx as one or two-digit and recursively do this, also maintain a memo to avoid re-calculation. Also, originally I’m using stoi(s.substr(s, 2)), where the chatGPT recommends to use (s[idx]-'0)*10 + s[idx+1] -'0', and I find that this approach is much faster. Code class Solution { public: int numDecodings(string s) { memo.assign(s.size(), -1); return decoding(s, 0); } int decoding(const string& s, int idx) { int n = s.size(); if(idx > n-1) return 0; int totalCnt = 0; if(memo[idx] != -1) return memo[idx]; if(s[idx] != '0') { totalCnt += idx == n-1 ? 1 : decoding(s, idx+1); } if( idx <= n-2 && (s[idx]-'0')*10 +(s[idx+1]-'0') <= 26 && s[idx] != '0') { totalCnt += idx == n-2 ? 1 : decoding(s, idx+2); } memo[idx] = totalCnt; return totalCnt; } vector<int> memo; };

February 21, 2026 · 1 min

LC 1092 - Shortest Common Supersequence

Approach This is a dp-like problem, and I was original thinking that for a - b - a - c, we try to match c - a - b as possible. when we match c, we find that it is not possible, when matching a, we would also match the first occurence, since matching a latter one would not be better. Also, we can try to not match the current character, and try to do insertion for this a. This can be recursively done by solvinf problem from the end. ...

January 31, 2026 · 4 min

LC 2977 - Minimum Cost to Convert String II

Approach This is a really hardr problem, originally I was thinking that for some string like a.. and b.., we must find somethiong to replace the first character, but I did not notice that the problme describe that the changing substring cannot be overlap (it can only be seperate or the same range). I originally think that ace as source and bcd as target, we might have a cost like ce to ed and aed to bcd, then we can combine both to transit to the target. ...

January 30, 2026 · 3 min

LC 3651 - Minimum Cost Path with Teleportations

Approach This is a really hard problem! the idea is easy but there are many case to consider, and also a second part for this problem is to do efficient teleporatoin computation. This is a dp like problme, we can think that when there are no teleport, we can calcualte the answer simply by checking each grid’s top and left. Lets call this answer matrix t0. When doing one teleportation, I was thinking that we can do teleportation from t0, but actually, we can also walk after the teleportation!! Therefore, we need to update t1 first based on t0, and then do the top/left traversal for the t1 again to update the optimal value. ...

January 30, 2026 · 3 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 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 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

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 3797 - Count Routes to Climb a Rectangular Grid

Approach This is a DP like problem, and I find that we can think that when we want to calculate ans[i][j] on row i from row i-1, we just aggregate the previous row’s ans within the l2 distance of d and we get the answer. But the problem stats that we can jump in the same row in range d, so I tried to buffer the row i-1 aggregated results to jump, and then try to sum up all the jump within distance d. Originally I think that for the index j itself we need to count twice, since we can jump from upper row, or jump from upper row and jump to itself again. However, from the testcase it seems that jump to itself is invalid. Under this circumstance we just add up all the jumps within range d and we got the answer for row i. ...

January 3, 2026 · 4 min