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

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

LC 3795 - Minimum Subarray Length With Distinct Sum At Least K

Approach This is quite easy to see that is a two pointer type of problem. One difficult part is that for the boundary condition, such as one elemenet only may need some carefull handling (I get WA on that). Also, the sum should remove duplicates so we need to store the count for each variable with a hashmap. Add when there is no this element in the range yet, and remove if this is the last one in the range! (also get 1 WA on this…) ...

January 3, 2026 · 1 min

LC 3794 - Reverse String Prefix

Approach This is really simple, just use the reverse() function. Time complexity is O(N), and no extra space complexity. Code class Solution { public: string reversePrefix(string s, int k) { reverse(s.begin(), s.begin()+k); return s; } };

January 3, 2026 · 1 min

LC 1358 - Number of Substrings Containing All Three Characters

[translate by ChatGPT] This problem is almost the same as the one from yesterday - it’s about finding substrings that contain characters at least a certain number of times. A two-pointer approach is sufficient to solve it successfully. Code class Solution { public: int numberOfSubstrings(string s) { int n = s.size(); vector<int> cnt(3, 0); int left = 0, right = 0, ans = 0; while(right < n) { ++cnt[s[right]-'a']; while(cnt[0] >= 1 && cnt[1] >= 1 && cnt[2] >= 1) { ans += (n - right); --cnt[s[left]-'a']; ++left; } ++right; } return ans; } };

March 11, 2025 · 1 min

LC 3306 - Count of Substrings Containing Every Vowel and K Consonants II

[translate by ChatGPT] This problem is really difficult. Even after reading the solution, it took me quite a while to realize that a two-pointer approach can be used to solve the “at least K” condition. In the end, we can compute the number of substrings with exactly k by taking the difference between “at least k” and “at least k + 1”. Code class Solution { public: long long countOfSubstrings(string word, int k) { return atLeast(word, k) - atLeast(word, k+1); } long atLeast(const string& word, const int& k) { int n = word.size(); int left = 0; int right = 0; vector<int> cnt(6, 0); long ans = 0; while(right < n) { ++cnt[getIdx(word[right])]; while(cnt[0] > 0 && cnt[1] > 0 && cnt[2] > 0 && cnt[3] > 0 && cnt[4] > 0 && cnt[5] >= k) { ans += (n - right); --cnt[getIdx(word[left])]; ++left; } ++right; } return ans; } inline int getIdx(char c) { switch(c) { case 'a': return 0; case 'e': return 1; case 'i': return 2; case 'o': return 3; case 'u': return 4; default : return 5; } } };

March 10, 2025 · 1 min

LC 3478 - Choose K Elements With Maximum Sum

[translate by ChatGPT] I actually thought of the solution early on, but at first I didn’t notice that the problem requires choosing k elements. I initially thought it only required finding the two largest values. Later I realized that I could implement it using a min heap. This also made me feel that I should review how to use a min heap again. The usage of priority_queue is actually quite simple: just use top() to get the element, and then push() / pop(). ...

March 9, 2025 · 2 min

LC 3479 - Fruits Into Baskets III

[translate by ChatGPT] This problem felt similar to the first one, but I misunderstood the problem statement at the beginning. I also feel that the description of the problem wasn’t very clear. Even toward the end I still wasn’t completely sure what the problem was trying to say. Even when I tried to solve it based on my interpretation, I still couldn’t come up with a solution. Later I saw that the official solution uses a segment tree, and only then did I understand the intended approach. At first I didn’t quite understand it, but eventually I figured out what the data structure is doing. ...

March 9, 2025 · 3 min

LC 1306 - Jump Game III

[translate by ChatGPT] In my first attempt, I didn’t notice the possibility of an infinite loop, so I needed to maintain a visited set. At the beginning I wasn’t very sure how to estimate the complexity of this problem. It feels like it should be O(n). My reasoning was that in the worst case every element is 1, and we start searching from the end all the way to the beginning. As long as we ensure that we never visit the same index twice, the complexity stays O(n). ...

March 8, 2025 · 1 min

LC 2523 - Closest Prime Numbers in Range

[translate by ChatGPT] It’s been a while since I worked on prime-related problems. At first, I thought I needed to store previously computed primes, but it turns out that wasn’t necessary. This problem has many boundary conditions. When dealing with primes, I need to be especially careful about handling 1 and 2 correctly. Also, when checking whether a number is prime, it’s enough to check up to sqrt(n). That’s much faster than checking up to n/2. ...

March 7, 2025 · 2 min