Skip to content

2592. Maximize Greatness of an Array 👍

  • Time: $O(\texttt{sort})$
  • Space: $O(\texttt{sort})$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  int maximizeGreatness(vector<int>& nums) {
    int ans = 0;

    ranges::sort(nums);

    for (const int num : nums)
      if (num > nums[ans])
        ++ans;

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
  public int maximizeGreatness(int[] nums) {
    int ans = 0;

    Arrays.sort(nums);

    for (final int num : nums)
      if (num > nums[ans])
        ++ans;

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution:
  def maximizeGreatness(self, nums: list[int]) -> int:
    ans = 0

    nums.sort()

    for num in nums:
      if num > nums[ans]:
        ans += 1

    return ans