LC 1984 - Minimum Difference Between Highest and Lowest of K Scores

Approach When select k score that have a minimum difference, we would like to make them as close as possible. Therefore, we sort the array and chekc all continuous k elements max difference (that is the i and i+k-1 element’s difference after sorting), ans find the minimum among them. Code class Solution { public: int minimumDifference(vector<int>& nums, int k) { sort(nums.begin(), nums.end()); int n = nums.size(); int minVal = nums[n-1] - nums[0]; for(int i = 0 ; i <= n-k ; ++i) { minVal = min(minVal, nums[i+k-1] - nums[i]); } return minVal; } };

January 25, 2026 · 1 min

LC 1877 - Minimize Maximum Pair Sum in Array

Approach This seems to be a difficult problem, but it is acutally simple. When we select pairs for a <= b <= c <= d, it is obvious that selecting (a,d) with (b,c) can achieve the minimax; therefore, we only need to sort the original array and find the max with two pointers from head and tali. Code class Solution { public: int minPairSum(vector<int>& nums) { sort(nums.begin(), nums.end()); int sum = INT_MIN; int n = nums.size(); for(int i = 0 ; i < n/2 ; ++i) { sum = max(sum, nums[i] + nums[n-1-i]); } return sum; } };

January 24, 2026 · 1 min

LC 3314 - Construct the Minimum Bitwise Array I

Approach I found that the answer for this is find the longest continuous 1’s in the number, and remove that 1. For instance, 1000111 -> 1000011. I also found an interesting thing to do this without using while loop to shift, the formula for this is n & ~((~n & (n+1)) >> 1). The part (~n & (n+1)) >> 1 is the highest continuos 1 bit since for n = ? 0 1 .. 1, we have n+1 = ? 1 0 .. 0, then ~n & (n+1) = 0 1 0 .. 0, shifting this by one is the highest bit. ...

January 20, 2026 · 1 min

LC 1975 - Maximum Matrix Sum

Approach This is a math like problem, I think that if a list is given we can always swap the first one to positive and propagate the operation. In the end, there might be no negative number or just a negative number in the end. This depends on the total number of negative numbers in the original case, since the operatoin would not change the odd/even for the total negative count, if original matrix is having odd count, the last element would be negative. ...

January 5, 2026 · 2 min

LC 1390 - Four Divisors

Approach Originally I thought that we just need to find the number that can be divided by two primes. num = prime1 * prime2, but got an WA, since I’ve ignored the num = prime1 * prime1 * prime1, such as 8 has 1, 2, 4, 8 as divisors. So the overall method is that we precalculate the prime and check the above conditions. My first submit is slow since I tried to build prime up to maxNum, but actually we can build to floor(sqrt(maxNum)). I was thinking that when prime1 is smaller in this square-root range, but prime2 would not, so I build the entire prime inside the whole range. Actually, even when prime2 is larger then the square-root range, we can walk the prime again and check if there are other divisors (this can reduce lots of complexity!). ...

January 4, 2026 · 2 min