2592.cpp (673B)
1 const auto _ = []() { 2 ios_base::sync_with_stdio(0); 3 cout.tie(NULL); 4 cin.tie(NULL); 5 return NULL; 6 }(); 7 8 // O(nlogn) 9 class Solution { 10 public: 11 int maximizeGreatness(vector<int> &nums) const { 12 sort(rbegin(nums), rend(nums)); 13 14 int res = 0; 15 for (int i = 0; i < size(nums); i++) { 16 if (nums[i] < nums[res]) res++; 17 } 18 19 return res; 20 } 21 }; 22 23 // O(n) 24 class Solution { 25 public: 26 int maximizeGreatness(const vector<int> &nums) const { 27 unordered_map<int, int> count; 28 int res = 0; 29 for (const int n : nums) 30 res = max(res, ++count[n]); 31 return size(nums) - res; 32 } 33 };