2587. Rearrange Array to Maximize Prefix Score Medium
1/**
2 * [2587] Rearrange Array to Maximize Prefix Score
3 *
4 * You are given a 0-indexed integer array nums. You can rearrange the elements of nums to any order (including the given order).
5 * Let prefix be the array containing the prefix sums of nums after rearranging it. In other words, prefix[i] is the sum of the elements from 0 to i in nums after rearranging it. The score of nums is the number of positive integers in the array prefix.
6 * Return the maximum score you can achieve.
7 *
8 * <strong class="example">Example 1:
9 *
10 * Input: nums = [2,-1,0,1,-3,3,-3]
11 * Output: 6
12 * Explanation: We can rearrange the array into nums = [2,3,1,-1,-3,0,-3].
13 * prefix = [2,5,6,5,2,2,-1], so the score is 6.
14 * It can be shown that 6 is the maximum score we can obtain.
15 *
16 * <strong class="example">Example 2:
17 *
18 * Input: nums = [-2,-3,0]
19 * Output: 0
20 * Explanation: Any rearrangement of the array will result in a score of 0.
21 *
22 *
23 * Constraints:
24 *
25 * 1 <= nums.length <= 10^5
26 * -10^6 <= nums[i] <= 10^6
27 *
28 */
29pub struct Solution {}
30
31// problem: https://leetcode.com/problems/rearrange-array-to-maximize-prefix-score/
32// discuss: https://leetcode.com/problems/rearrange-array-to-maximize-prefix-score/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36impl Solution {
37 pub fn max_score(nums: Vec<i32>) -> i32 {
38 0
39 }
40}
41
42// submission codes end
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47
48 #[test]
49 fn test_2587() {
50 }
51}
52
Back
© 2025 bowen.ge All Rights Reserved.