Leetcode 2587. Rearrange Array to Maximize Prefix Score

2023-03-12 11:58:31     来源:哔哩哔哩

You are given a 0-indexed integer array nums. You can rearrange the elements of numsto any order (including the given order).


(资料图片)

Let prefixbe the array containing the prefix sums of numsafter rearranging it. In other words, prefix[i]is the sum of the elements from 0to iin numsafter rearranging it. The score of numsis the number of positive integers in the array prefix.

Return the maximum score you can achieve.

Example 1:

Input: nums = [2,-1,0,1,-3,3,-3]

Output: 6

Explanation: We can rearrange the array into nums = [2,3,1,-1,-3,0,-3].prefix = [2,5,6,5,2,2,-1], so the score is 6.It can be shown that 6 is the maximum score we can obtain.

Example 2:

Input: nums = [-2,-3,0]

Output: 0

Explanation: Any rearrangement of the array will result in a score of 0.

Constraints:

1 <= nums.length <= 105

-106 <= nums[i] <= 106

忘记int溢出的情况了,要是早点用long,也不至于只AC一道题。。。。

Runtime: 34 ms, faster than 100.00% of Java online submissions for Rearrange Array to Maximize Prefix Score.

Memory Usage: 59.3 MB, less than 100.00% of Java online submissions for Rearrange Array to Maximize Prefix Score.

标签:

包装