LC 417 - Pacific Atlantic Water Flow

We walk from top left with BFS, and neighbors should be larger or equal to its current height. Do the same thing for bottom and right, and in the end find the intersections. Code class Solution { public: vector<vector<int>> pacificAtlantic(vector<vector<int>>& h) { int m = h.size(); int n = h[0].size(); queue<pair<int,int>> qPacific; queue<pair<int,int>> qAtlantic; vector<vector<bool>> pacific(m, vector<bool>(n, false)); vector<vector<bool>> atlantic(m, vector<bool>(n, false)); for(int i = 0 ; i < n ; ++i) { qPacific.push({0, i}); qAtlantic.push({m-1, i}); } for(int i = 0 ; i < m ; ++i) { qPacific.push({i, 0}); qAtlantic.push({i, n-1}); } traverse(qPacific, h, pacific); traverse(qAtlantic, h, atlantic); vector<vector<int>> ans; for(int i = 0 ; i < m ; ++i) { for(int j = 0 ; j < n ; ++j) { if(pacific[i][j] && atlantic[i][j]) { ans.push_back({i, j}); } } } return ans; } void traverse(queue<pair<int,int>>& q, const vector<vector<int>>& h, vector<vector<bool>>& visit) { const vector<int> d = {0, 1, 0, -1, 0}; int m = visit.size(); int n = visit[0].size(); while(!q.empty()) { auto [x, y] = q.front(); q.pop(); if(visit[x][y]) continue; visit[x][y] = true; for(int i = 0 ; i < 4 ; ++i) { int nx = x + d[i]; int ny = y + d[i+1]; if(nx < 0 || nx > m-1) continue; if(ny < 0 || ny > n-1) continue; if(h[nx][ny] < h[x][y]) continue; q.push({nx, ny}); } } } };

February 27, 2026 · 2 min

LC 152 - Maximum Product Subarray

We split the vector into several segments based on the location of zero, and then calculate the max product for each segment (notice that the end one should always be end of segment). The max product for segment is calculated in getProd, and this is done by first counting negative numbers, if there are even numbers we multiply all numbers to get the largest product, if there are odd number of negative value, we end before the last negative value (prod1) or start after the first negative value (prod2). ...

February 25, 2026 · 2 min

LC 1022 - Sum of Root To Leaf Binary Numbers

traverse the tree and maintain current number, double the value when approaching next level, and sum the value up when reach leaf. Code class Solution { public: int sumRootToLeaf(TreeNode* root) { int sum = 0; traverse(0, root, sum); return sum; } void traverse(int n, TreeNode* root, int& sum) { n = (n << 1) + root->val; if(root->left) traverse(n, root->left, sum); if(root->right) traverse(n, root->right, sum); if(root->left == nullptr && root->right == nullptr) sum += n; } };

February 24, 2026 · 1 min

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 868 - Binary Gap

Store the binary representaiton and then find the max distance betweeb location of 1’s. Code class Solution { public: int binaryGap(int n) { deque<char> s; while(n > 0) { if(n % 2 == 1) s.push_front('1'); else s.push_front('0'); n /= 2; } vector<int> idx; for(int i = 0 ; i < s.size() ; ++i) if(s[i] == '1') idx.push_back(i); if(idx.size() <= 1) return 0; int maxWidth = 1; for(int i = 1 ; i < idx.size() ; ++i) maxWidth = max(maxWidth, idx[i] - idx[i-1]); return maxWidth; } };

February 22, 2026 · 1 min

LC 3849 - Maximum Bitwise XOR After Rearrangement

We record the position for ones and zeros, then count the number of ones in t, the goal is to match the 1’s in t to some positions in s. The stategy is to (1) mathc the zeros from MSB, and (2) match the zeros from LSB, since matching zeros would increase the value, so we want to do this from MSB, and for mathcing ones we decreas the value so we want to do this from LSB. ...

February 22, 2026 · 2 min

LC 3848 - Check Digitorial Permutation

We pre-calculate the factorial for 1 to 10 first, then iterate thorught the string and add all the factorial up. Now we sum then up, and chekc if this sum can be formed by the original numers digits. Code class Solution { public: bool isDigitorialPermutation(int n) { vector<int> fac(10, 1); for(int i = 1 ; i < 10 ; ++i) fac[i] = fac[i-1] * i; string s = to_string(n); int cnt = s.size(); int sum = 0; for(int i = 0 ; i < cnt ; ++i) { sum += fac[s[i] - '0']; } string s2 = to_string(sum); if(s2.size() != s.size()) return false; unordered_map<char, int> charCnt; for(const auto& c: s) ++charCnt[c]; for(const auto& c: s2) { if(charCnt[c] == 0) return false; --charCnt[c]; } return true; } };

February 22, 2026 · 1 min

LC 3847 - Find the Score Difference in a Game

Check if the current number is odd, and the idx is mod 5 by 6, then update score1 and score2 Code class Solution { public: int scoreDifference(vector<int>& nums) { int score1 = 0; int score2 = 0; bool active1 = true; int n = nums.size(); for(int i = 0 ; i < n ; ++i) { if((nums[i] % 2 == 1) ^ (i % 6 == 5)) active1 = !active1; if(active1) score1 += nums[i]; else score2 += nums[i]; } return score1 - score2; } };

February 22, 2026 · 1 min

LC 100 - Same Tree

check the currnet node value, and then check the status for left and right child with the function itself recursively. Code class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if (p == nullptr && q == nullptr) return true; else if (p == nullptr || q == nullptr) return false; if (p->val != q->val) return false; if ((p->left == nullptr) ^ (q->left == nullptr)) return false; else if (!isSameTree(p->left, q->left)) return false; if((p->right == nullptr) ^ (q->right == nullptr)) return false; else if (!isSameTree(p->right, q->right)) return false; return true; } };

February 21, 2026 · 1 min

LC 347 - Top K Frequent Elements

Count hte frequency, and sort by the freq in descending order, then return the first k number. Code class Solution { public: vector<int> topKFrequent(vector<int>& nums, int k) { unordered_map<int, int> freq; for(const auto& x: nums) ++freq[x]; vector<pair<int,int>> v; for(const auto& [x, cnt]: freq) v.push_back({cnt, x}); sort(v.begin(), v.end(), greater<pair<int,int>>()); vector<int> ans; ans.reserve(k); for(int i = 0 ; i < k ; ++i) ans.push_back(v[i].second); return ans; } };

February 21, 2026 · 1 min