#2592
Maximize Greatness of an Array
specialist · 695 · lc medium +30 · verified · 61.3% accepted · 505 likes · top 61%
Description
You are given a 0-indexed integer array nums. Choose any permutation perm of nums. The greatness is the count of indices i for which perm[i] > nums[i]. Return the maximum possible greatness.
Example 1:
Input: nums = [1,3,5,2,1,3,1]
Output: 4
Explanation: One of the optimal rearrangements is perm = [2,5,1,3,3,1,1].
At indices = 0, 1, 3, and 4, perm[i] > nums[i]. Hence, we return 4.
Example 2:
Input: nums = [1,2,3,4]
Output: 3
Explanation: We can prove the optimal perm is [2,3,4,1].
At indices = 0, 1, and 2, perm[i] > nums[i]. Hence, we return 3.
Code
1
2
3