Skip to content
Rain Hu's Workspace
Go back

[LeetCode] 2779. Maximum Beauty of an Array After Applying Operation

Rain Hu
class Solution {
public:
    int maximumBeauty(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());
        int res = 0, left = 0, right = 0, n = nums.size();
        while (right < n) {
            int num = nums[right++];
            while (num - nums[left] > 2 * k) left++;
            res = max(res, right - left);
        }
        return res;
    }
};

Share this post on:

Previous
[LeetCode] 20. Valid Parentheses
Next
[LeetCode] 2958. Length of Longest Subarray With at Most K Frequency