LC - KMP alogirthms

Recently practicing hte KMP algorithm, and I think this is really interesting. This alogirthm is a linear time method to find if a string s (say size m) is a substring of t (say size n). A naive approach is to do comparison for each characters using two loops, and the complexity is O(m*n), while using the KMP algorithm, we can reuse the property of string s by pre-building a LSP (longest suffix prefix) with linear time O(m). In this case the comparison can be done in O(n). ...

January 15, 2026 · 4 min

LC 3453 - Separate Squares I

Approach I was thinking that this problem is the same as: giving a list of [start, end, height] rectengle, and you need to find hte value that cut them in half, and this is the line swepping algorithm (in Code-1) I also saw another interesting approach is to use the binary search (actually serach on answer) within a given error rnage (implemented in Code-2) Code-1: Line sweeping class Solution { public: double separateSquares(vector<vector<int>>& squares) { int n = squares.size(); double totalArea = 0; for_each(squares.begin(), squares.end(), [&](auto& v){totalArea += (double)v[2]*v[2];}); totalArea /= 2; map<int, int> trigger; // coord(Y) -> weight. for(int i = 0 ; i < n ; ++i) { int start = squares[i][1]; int end = squares[i][1] + squares[i][2]; int height = squares[i][2]; trigger[start] += height; trigger[end] -= height; } int curH = 0; int curY = -1; int prevY = -1; for(const auto& e: trigger) { curY = e.first; double curArea = (double) (curY - prevY) * curH; totalArea -= curArea; if(totalArea <= 0) break; curH += e.second; prevY = curY; } return curY + totalArea / curH; } }; Code-2: Binray search within error class Solution { public: double separateSquares(vector<vector<int>>& squares) { double totalArea = 0; for_each(squares.begin(), squares.end(), [&](auto& v){totalArea += (double)v[2]*v[2];}); totalArea /= 2; double l = 0; double r = 1'000'000'000; while(r - l >= 1e-5) { double m = l + (r - l) / 2; if(getAreaBelow(squares, m) < totalArea) { l = m; } else { r = m; } } return l; } inline double getAreaBelow(const vector<vector<int>>& squares, double h) { double area = 0; for(const auto& sq: squares) { if(double(sq[1]) >= h) continue; if(double(sq[1]+sq[2]) <= h) { area += (double)sq[2]*sq[2]; } else { area += double(h-sq[1])*sq[2]; } } return area; } };

January 13, 2026 · 2 min

LC - Floyds slow fast pointer

Detect Loop Recently I practice the topic for floyd’s fast slow pointer algorithm to detect loop. The basic problem is that there is a link list with cycle (that is each node only points to at most one node), we can detect it by walking a fast pointer (do two ->next per loop), and slow pointer (one ->next at a time). In this case, there is a loop if two pointers can meet each other. ...

January 13, 2026 · 3 min

LC 1266 - Minimum Time Visiting All Points

Approach since we need to walk point in order, just process one by one, and for each one the distance is max of the absolute difference in x and y direction. Code class Solution { public: int minTimeToVisitAllPoints(vector<vector<int>>& points) { int dist = 0; for(int i = 1 ; i < points.size() ; ++i) { int dx = abs(points[i][0] - points[i-1][0]); int dy = abs(points[i][1] - points[i-1][1]); dist += max(dx, dy); } return dist; } };

January 12, 2026 · 1 min

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 3803 - Count Residue Prefixes

Approach this weeks weekly, problem 1 is simple, we maintain unordered_set to check how many unqiue values and chekc hte current length. Code class Solution { public: int residuePrefixes(string s) { unordered_set<char> st; int resi = 0; for(int i = 0 ; i < s.size() ; ++i) { st.insert(s[i]); if(st.size() == (i+1) % 3) ++ resi; } return resi; } };

January 11, 2026 · 1 min

LC 3804 - Number of Centered Subarrays

Approach problem 2 can also be done using hash and prefixsum, timing complexity is O(N^2) Code class Solution { public: int centeredSubarrays(vector<int>& nums) { int n = nums.size(); int totalCnt = 0; for(int i = 0 ; i < n ; ++i){ unordered_set<int> st; int sum = 0; for(int j = i ; j < n ; ++j) { sum += nums[j]; st.insert(nums[j]); if(st.count(sum)) ++totalCnt; } } return totalCnt; } };

January 11, 2026 · 1 min

LC 3805 - Count Caesar Cipher Pairs

Approach Try to let the first char to become ‘a’, and then compare how many strings are the same for each one (with hashmap). Code class Solution { public: long long countPairs(vector<string>& words) { int m = words.size(); int n = words[0].size(); for(int i = 0 ; i < m ; ++i) { int shift = words[i][0] - 'a'; for(int j = 0 ; j < n ; ++j) { int curShift = (int(words[i][j] - 'a') - shift + 26 ) % 26; words[i][j] = ('a' + curShift); } } unordered_map<string, int> mp; long long totalCnt = 0; for(int i = 0 ; i < m ; ++i) { if(mp.count(words[i])) totalCnt += mp[words[i]]; ++mp[words[i]]; } return totalCnt; } };

January 11, 2026 · 1 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 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