---
# System prepended metadata

title: 2592. Maximize Greatness of an Array
tags: [Leetcode, Two Pointers, Medium, Greedy]

---

# 2592. Maximize Greatness of an Array
###### tags: `Leetcode` `Medium` `Two Pointers` `Greedy`
Link: https://leetcode.com/problems/maximize-greatness-of-an-array/description/
## 思路
思路参考[这里](https://leetcode.com/problems/maximize-greatness-of-an-array/solutions/3312061/java-c-python-easy-and-concise-o-n/)
### Two Pointers $O(NlogN)$ $O(1)$
### Greedy $O(N)$ $O(N)$
把这个array想象成已经sorted的array
我们希望找到两个subsequence 
seq1: ```nums[0], nums[1], ..., nums[n-1-k]```
seq2: ```nums[k], nums[k+1], ..., nums[n-1]```
我们希望```nums[0]<nums[k]```, ```nums[1]<nums[k+1]```, ... , ```nums[n-1-k]<nums[n-1]```
这样greatness就是```n-k```
那么怎么找```k```呢 其实我们只要找到```nums```里面每个数字出现的最大频率即可 那就是最小的```k```
因为如果```k```小于```nums```里面数字出现的最大频率 ```seq1```和```seq2```里面肯定有一个数字是一样的
## Code
### Two Pointers
```java=
class Solution {
    public int maximizeGreatness(int[] nums) {
        Arrays.sort(nums);
        int res = 0;
        for(int num:nums){
            if(num > nums[res]){
                res++;
            }
        }
        return res;
    }
}
```
### Greedy
```python=
class Solution:
    def maximizeGreatness(self, nums: List[int]) -> int:
        return len(nums)-max(Counter(nums).values())
```