返回介绍

solution / 2200-2299 / 2219.Maximum Sum Score of Array / README_EN

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

2219. Maximum Sum Score of Array

中文文档

Description

You are given a 0-indexed integer array nums of length n.

The sum score of nums at an index i where 0 <= i < n is the maximum of:

  • The sum of the first i + 1 elements of nums.
  • The sum of the last n - i elements of nums.

Return _the maximum sum score of _nums_ at any index._

 

Example 1:

Input: nums = [4,3,-2,5]
Output: 10
Explanation:
The sum score at index 0 is max(4, 4 + 3 + -2 + 5) = max(4, 10) = 10.
The sum score at index 1 is max(4 + 3, 3 + -2 + 5) = max(7, 6) = 7.
The sum score at index 2 is max(4 + 3 + -2, -2 + 5) = max(5, 3) = 5.
The sum score at index 3 is max(4 + 3 + -2 + 5, 5) = max(10, 5) = 10.
The maximum sum score of nums is 10.

Example 2:

Input: nums = [-3,-5]
Output: -3
Explanation:
The sum score at index 0 is max(-3, -3 + -5) = max(-3, -8) = -3.
The sum score at index 1 is max(-3 + -5, -5) = max(-8, -5) = -5.
The maximum sum score of nums is -3.

 

Constraints:

  • n == nums.length
  • 1 <= n <= 105
  • -105 <= nums[i] <= 105

Solutions

Solution 1

class Solution:
  def maximumSumScore(self, nums: List[int]) -> int:
    s = [0] + list(accumulate(nums))
    return max(max(s[i + 1], s[-1] - s[i]) for i in range(len(nums)))
class Solution {
  public long maximumSumScore(int[] nums) {
    int n = nums.length;
    long[] s = new long[n + 1];
    for (int i = 0; i < n; ++i) {
      s[i + 1] = s[i] + nums[i];
    }
    long ans = Long.MIN_VALUE;
    for (int i = 0; i < n; ++i) {
      ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i]));
    }
    return ans;
  }
}
class Solution {
public:
  long long maximumSumScore(vector<int>& nums) {
    int n = nums.size();
    vector<long long> s(n + 1);
    for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i];
    long long ans = INT_MIN;
    for (int i = 0; i < n; ++i) ans = max(ans, max(s[i + 1], s[n] - s[i]));
    return ans;
  }
};
func maximumSumScore(nums []int) int64 {
  n := len(nums)
  s := make([]int64, n+1)
  for i, v := range nums {
    s[i+1] = s[i] + int64(v)
  }
  var ans int64 = math.MinInt64
  for i := 0; i < n; i++ {
    ans = max(ans, max(s[i+1], s[n]-s[i]))
  }
  return ans
}
function maximumSumScore(nums: number[]): number {
  const n = nums.length;
  let s = new Array(n + 1).fill(0);
  for (let i = 0; i < n; ++i) {
    s[i + 1] = s[i] + nums[i];
  }
  let ans = -Infinity;
  for (let i = 0; i < n; ++i) {
    ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i]));
  }
  return ans;
}
/**
 * @param {number[]} nums
 * @return {number}
 */
var maximumSumScore = function (nums) {
  const n = nums.length;
  let s = new Array(n + 1).fill(0);
  for (let i = 0; i < n; ++i) {
    s[i + 1] = s[i] + nums[i];
  }
  let ans = -Infinity;
  for (let i = 0; i < n; ++i) {
    ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i]));
  }
  return ans;
};

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

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

发布评论

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