环球即时看!Leetcode 2592. Maximize Greatness of an Array
来源:  哔哩哔哩
时间:  2023-03-19 13:05:18

You are given a 0-indexed integer array nums. You are allowed to permute numsinto a new array permof your choosing.

We define the greatness of numsbe the number of indices 0 <= i < nums.lengthfor which perm[i] > nums[i].

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


(相关资料图)

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.

想啊想,用队列还是优先队列。。。结果不需要额外的空间,自己跟跟自己去比对,2个指针就可以。。。我真的还是太笨了

Runtime: 11 ms, faster than 100.00% of Java online submissions for Maximize Greatness of an Array.

Memory Usage: 59.4 MB, less than 100.00% of Java online submissions for Maximize Greatness of an Array.

标签:

猜你喜欢