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 3823 - Reverse Letters Then Special Characters in a String

Approach I store the lowercase and non-lowercase in two stacks, and re-assign each with reverse order by poping from the stack. Code class Solution { public: string reverseByType(string s) { stack<char> stk1; stack<char> stk2; int n = s.size(); for(int i = 0 ; i < n ; ++i) { if(s[i] >= 'a' && s[i] <= 'z') stk1.push(s[i]); else stk2.push(s[i]); } for(int i = 0 ; i < n ; ++i) { if(s[i] >= 'a' && s[i] <= 'z') { s[i] = stk1.top(); stk1.pop(); } else { s[i] = stk2.top(); stk2.pop(); } } return s; } };

January 31, 2026 · 1 min