LC 3824 - Minimum K to Reduce Array Within Limit

Approach This is a typical search on answer problme, we try to see if the answer is valid, this can be done in simple math by O(N), and the binary search on lgN. Code class Solution { public: int minimumK(vector<int>& nums) { bool isAllNegative = true; int n = nums.size(); for(int i = 0 ; i < n ; ++i) { if(nums[i] > 0) { isAllNegative = false; break; } } if(isAllNegative) return 0; int l = 1; int r = 100000; while(l < r) { int m = l + (r - l) / 2; if(isValid(nums, m)) { r = m; } else { l = m + 1; } } return l; } bool isValid(const vector<int>& v, int k) { int n = v.size(); int cnt = 0; for(int i = 0 ; i < n ; ++i) { if(v[i] <= 0) continue; cnt += v[i] / k; if(v[i] % k != 0) ++cnt; } return (long long)cnt <= (long long)k*k; } };

January 31, 2026 · 1 min

LC 1292 - Maximum Side Length of a Square with Sum Less than or Equal to Threshold

Approach I think that we can do a O(N^3) approach by maintaining prefix sum, and then do post processing to get the currnet sume for all pairs of square from i,j to i+k,i+k. My first version was slow where I use k=1 to end, it can be much faster by changing it to k=maxWith+1 to end; however, the worst case comeplxity is still large. An interseting part is the 2D prefix, it can be done easily by adding in the horizontal direction then the vertical direction. ...

January 19, 2026 · 3 min

LC 295 - Find Median from Data Stream

Approach I waw originally thinking that we can insert the number with binary search (or lower_bound), and find median with directly taking the number from index for the vector. This strategy can pass the porlbme, but this is actually an O(N) solution since vector insert can be O(n). I then find out that we can maintain a minheap and maxheap with O(lgN) insertion time, and we only need to rebalance two heaps when their size is different by more than 2. ...

January 9, 2026 · 2 min