2592. Maximize Greatness of an Array

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int maximizeGreatness(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
int l = 0 ,r = nums.size()-1;
int i = n-1;
while(l<=r) {
if(nums[i] >= nums[r]) l++; // 打不過就派最小的
else r--;
i--;
}
return n-r-1;
}
};

analysis

  • time complexity O(nlogn)
  • space complexity O(1)