LC 3047 - Find the Largest Area of Square Inside Two Rectangles

Approach We check all pairs of the square and find its intersection, this can be done easily by maintaingin the minimum for top and right, and maximum for bottom and left, and then we can somply compare the width for that intersection area. Code class Solution { public: long long largestSquareArea(vector<vector<int>>& bottomLeft, vector<vector<int>>& topRight) { int n = bottomLeft.size(); int maxWidth = 0; for(int i = 0 ; i < n ; ++i) { for(int j = i+1 ; j < n ; ++j) { int left = max(bottomLeft[i][0], bottomLeft[j][0]); int right = min(topRight[i][0], topRight[j][0]); if(right <= left) continue; int bottom = max(bottomLeft[i][1], bottomLeft[j][1]); int top = min(topRight[i][1], topRight[j][1]); if(top <= bottom) continue; maxWidth = max(maxWidth, min(right-left, top-bottom)); } } return (long long)maxWidth * maxWidth; } };

January 17, 2026 · 1 min

LC 2975 - Maximum Square Area by Removing Fences From a Field

Approach We compare all pairs betweeb H and V pairs, and then store them in two vectors and then check the duplicate ones for eahc two vectors and that is the square that it can be formed. But this method show be optimizzed further since the two vector is in O(N^2) order, and comparing them both require extra times. Acutally we only need to collect pairs for either H or V (using a hash), and then when we are doing the pairs in the other direction, we can check if it is already in the hash dircetly . ...

January 16, 2026 · 2 min

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 2943 - Maximize Area of Square Hole in Grid

Approach We find the longest consecutive in both dimension, and take the minimum one out of two dimension, and that is the best square width. Code class Solution { public: int maximizeSquareHoleArea(int n, int m, vector<int>& hBars, vector<int>& vBars) { int side = min(longestConsecutivePlus1(hBars), longestConsecutivePlus1(vBars)); return side * side; } static int longestConsecutivePlus1(vector<int>& a) { if (a.empty()) return 1; sort(a.begin(), a.end()); int best = 1; int run = 1; for (int i = 1; i < a.size(); ++i) { if (a[i] == a[i-1] + 1) ++run; else run = 1; best = max(best, run); } return best + 1; } };

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