[Medium] 2592. Maximize Greatness of an Array
[Medium] 2592. Maximize Greatness of an Array
Given an array nums, permute it into perm to maximize:
count(i) where perm[i] > nums[i]
Return that maximum count (called greatness).
Core idea
Sort nums. Then greedily match a larger element to beat each smaller element.
- Let
ipoint to the smallest value we still need to beat. - Scan sorted values
xfrom left to right. - If
x > nums[i], usexto beat it and incrementi.
At the end, i is the answer.
Why this works:
- Using the smallest possible winner for each target preserves larger numbers for harder matches later.
- If a value cannot beat the current smallest target, it cannot beat any later target either.
Python
from typing import List
class Solution:
def maximizeGreatness(self, nums: List[int]) -> int:
nums.sort()
i = 0
for x in nums:
if x > nums[i]:
i += 1
return i
Example
nums = [1, 3, 5, 2, 1, 3, 1]
Sorted: [1, 1, 1, 2, 3, 3, 5]
1cannot beat11cannot beat11cannot beat12beats1->i = 13beats1->i = 23beats1->i = 35beats2->i = 4
Answer: 4.
Complexity
- Time:
O(n log n)due to sorting - Space:
O(1)extra (ignoring sort internals)
Pattern
Greedy matching after sorting (two pointers / sweep matching).