返回介绍

solution / 2500-2599 / 2587.Rearrange Array to Maximize Prefix Score / README_EN

发布于 2024-06-17 01:03:03 字数 4413 浏览 0 评论 0 收藏 0

2587. Rearrange Array to Maximize Prefix Score

中文文档

Description

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

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.

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

Solutions

Solution 1: Greedy + Sorting

To maximize the number of positive integers in the prefix sum array, we need to make the elements in the prefix sum array as large as possible, that is, to add as many positive integers as possible. Therefore, we can sort the array $nums$ in descending order, then traverse the array, maintaining the prefix sum $s$. If $s \leq 0$, it means that there can be no more positive integers in the current position and the positions after it, so we can directly return the current position.

Otherwise, after the traversal, we return the length of the array.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $nums$.

class Solution:
  def maxScore(self, nums: List[int]) -> int:
    nums.sort(reverse=True)
    s = 0
    for i, x in enumerate(nums):
      s += x
      if s <= 0:
        return i
    return len(nums)
class Solution {
  public int maxScore(int[] nums) {
    Arrays.sort(nums);
    int n = nums.length;
    long s = 0;
    for (int i = 0; i < n; ++i) {
      s += nums[n - i - 1];
      if (s <= 0) {
        return i;
      }
    }
    return n;
  }
}
class Solution {
public:
  int maxScore(vector<int>& nums) {
    sort(nums.rbegin(), nums.rend());
    long long s = 0;
    int n = nums.size();
    for (int i = 0; i < n; ++i) {
      s += nums[i];
      if (s <= 0) {
        return i;
      }
    }
    return n;
  }
};
func maxScore(nums []int) int {
  sort.Ints(nums)
  n := len(nums)
  s := 0
  for i := range nums {
    s += nums[n-i-1]
    if s <= 0 {
      return i
    }
  }
  return n
}
function maxScore(nums: number[]): number {
  nums.sort((a, b) => a - b);
  const n = nums.length;
  let s = 0;
  for (let i = 0; i < n; ++i) {
    s += nums[n - i - 1];
    if (s <= 0) {
      return i;
    }
  }
  return n;
}
impl Solution {
  pub fn max_score(mut nums: Vec<i32>) -> i32 {
    nums.sort_by(|a, b| b.cmp(a));
    let mut s: i64 = 0;
    for (i, &x) in nums.iter().enumerate() {
      s += x as i64;
      if s <= 0 {
        return i as i32;
      }
    }
    nums.len() as i32
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文