2592. Maximize Greatness of an Array Medium

@problem@discussion
#Array#Two Pointers#Greedy#Sorting



1/**
2 * [2592] Maximize Greatness of an Array
3 *
4 * You are given a 0-indexed integer array nums. You are allowed to permute nums into a new array perm of your choosing.
5 * We define the greatness of nums be the number of indices 0 <= i < nums.length for which perm[i] > nums[i].
6 * Return the maximum possible greatness you can achieve after permuting nums.
7 *  
8 * <strong class="example">Example 1:
9 * 
10 * Input: nums = [1,3,5,2,1,3,1]
11 * Output: 4
12 * Explanation: One of the optimal rearrangements is perm = [2,5,1,3,3,1,1].
13 * At indices = 0, 1, 3, and 4, perm[i] > nums[i]. Hence, we return 4.
14 * <strong class="example">Example 2:
15 * 
16 * Input: nums = [1,2,3,4]
17 * Output: 3
18 * Explanation: We can prove the optimal perm is [2,3,4,1].
19 * At indices = 0, 1, and 2, perm[i] > nums[i]. Hence, we return 3.
20 * 
21 *  
22 * Constraints:
23 * 
24 * 	1 <= nums.length <= 10^5
25 * 	0 <= nums[i] <= 10^9
26 * 
27 */
28pub struct Solution {}
29
30// problem: https://leetcode.com/problems/maximize-greatness-of-an-array/
31// discuss: https://leetcode.com/problems/maximize-greatness-of-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35impl Solution {
36    pub fn maximize_greatness(nums: Vec<i32>) -> i32 {
37        0
38    }
39}
40
41// submission codes end
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn test_2592() {
49    }
50}
51


Back
© 2025 bowen.ge All Rights Reserved.