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

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 696 - Count Binary Substrings

I count the consecutive 1’s and 0’s, and for each transition, we take the minimum number of 1’s and 0’s, and this is the number of words that can be formed in this transtiion. Code class Solution { public: int countBinarySubstrings(string s) { int n = s.size(); vector<int> cnt; cnt.reserve(n); int zero = 0; int one = 0; for(int i = 0 ; i < n ; ++i) { if(s[i] == '1') { if(zero != 0) { cnt.push_back(zero); zero = 0; } ++one; } else { if(one != 0) { cnt.push_back(one); one = 0; } ++zero; } } if(zero != 0) cnt.push_back(zero); if(one != 0) cnt.push_back(one); int m = cnt.size(); int totalCnt = 0; for(int i = 1 ; i < m ; ++i) { totalCnt += min(cnt[i-1], cnt[i]); } return totalCnt; } };

February 19, 2026 · 1 min

LC 3719 - Longest Balanced Subarray I

Approach Do it in O(N^2) that count all the even and odd number count in each range [i .. j] with the help of hash, which would only store once for duplicate numbers. Code class Solution { public: int longestBalanced(vector<int>& nums) { int n = nums.size(); int maxLen = 0; for(int i = 0 ; i < n ; ++i) { unordered_set<int> even; unordered_set<int> odd; for(int j = i ; j < n ; ++j) { if(nums[j] % 2 == 1) odd.insert(nums[j]); else even.insert(nums[j]); if(even.size() == odd.size()) maxLen = max(maxLen, j-i+1); } } return maxLen; } };

February 10, 2026 · 1 min

LC 1382 - Balance a Binary Search Tree

Approach To rebuild the balacne tree, I stored the inorder traversal in the vector, and then break all the left and right connection (this step is important!), and then connecting them again by spliting the nodes into two clusters having similar number of nodes. Code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: TreeNode* balanceBST(TreeNode* root) { vector<TreeNode*> inorder; traverse(inorder, root); int n = inorder.size(); for(TreeNode* node: inorder) node->left = node->right = nullptr; return rebuild(inorder, 0, n-1); } void traverse(vector<TreeNode*>& inorder, TreeNode* root) { if(root->left) traverse(inorder, root->left); inorder.push_back(root); if(root->right) traverse(inorder, root->right); } TreeNode* rebuild(const vector<TreeNode*>& inorder, int start, int end) { if(start == end) return inorder[start]; if(end < start) return nullptr; int m = start + (end - start) / 2; TreeNode* curNode = inorder[m]; curNode->left = rebuild(inorder, start, m-1); curNode->right = rebuild(inorder, m+1, end); return curNode; } };

February 9, 2026 · 1 min

LC 338 - Counting Bits

Approach Need to couting bits for each n, and this is actually the number of bits for n/2 and plusing n%2. Code class Solution { public: vector<int> countBits(int n) { if(n == 0) return {0}; if(n == 1) return {0, 1}; vector<int> ans(n+1); ans[0] = 0; ans[1] = 1; for(int i = 2 ; i <= n ; ++i) { ans[i] = ans[i/2] + (i % 2); } return ans; } };

February 8, 2026 · 1 min

LC 1653 - Minimum Deletions to Make String Balanced

In this problme we need to removal character from string aab...bab containing only a and b, and make sure in the end it becomes aa..aab...b, find the minimum removal. I tried to count the prefix for how many b presents, and postifx for how many a presents, then when we want to seperate the string into two clusters, [.. idx] and [idx+1 ..], and make the first clsuter all a, and the second all b, we only need to add up prefBandpostA`. we check and maintan the best answer. ...

February 7, 2026 · 1 min

LC 3634 - Minimum Removals to Balance Array

Approach We sort the array first, and then find where the k times of the current value will be and update the best solution. I think we can do two pointer, or early termination when the current removal is already larger then best. Code class Solution { public: int minRemoval(vector<int>& nums, int k) { int n = nums.size(); int minRemoval = n; sort(nums.begin(), nums.end()); for(int i = 0 ; i < n ; ++i) { long long maxVal = (long long)k * nums[i]; if(maxVal > INT_MAX) maxVal = INT_MAX; int idx = upper_bound(nums.begin(), nums.end(), maxVal) - nums.begin(); int removal = n - idx; minRemoval = min(minRemoval, removal+i); } return minRemoval; } };

February 6, 2026 · 1 min

LC 3013 - Divide an Array Into Subarrays With Minimum Cost II

Approach Given a solution [ group A ] [i1 .. ] .. [ .. ] [i(k-1)], the distance between i1 and i(k-1)should be smaller then dist, and we assume that now the group A is fixed. The problem now is that we need to findk-2minimum elements in the range[i1 .. i(k-1)](excludingi1` itself), this part is similar to previous daily. Now we tried to do a sliding window, and we the current score is the first element of group A, plus the first element within the sliding window (dist+1 wide), and the sum of k-2 minimum value inside that sliding window (exlude the first one). We maintain the best score and we get the answer after sliding window. ...

February 2, 2026 · 2 min

LC 3827 - Count Monobit Integers

Approach Simple problem, for all 0, it will be zero, and for all 1, it will be 2^i-1, so we collect all the 2^i-1 within the range. Code class Solution { public: int countMonobit(int n) { int idx = 0; while(true) { int cur = (1 << idx) - 1; if(cur > n) break; ++idx; } return idx; } };

February 1, 2026 · 1 min