[LeetCode] Maximize Greatness of an Array

2592. Maximize Greatness of an Array

You are given a 0-indexed integer array nums. You are allowed to permute nums into a new array perm of your choosing.

We define the greatness of nums be the number of indices 0 <= i < nums.length for which perm[i] > nums[i].

Return the maximum possible greatness you can achieve after permuting nums.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int maximizeGreatness(vector<int>& nums) {
int res = 0;
multiset<int> ms;
for(auto a : nums) ms.insert(a);
for(int a : nums) {
auto it = ms.upper_bound(a);
if(it == end(ms)) continue;
ms.erase(it);
res += 1;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/03/18/PS/LeetCode/maximize-greatness-of-an-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.