返回介绍

solution / 2600-2699 / 2640.Find the Score of All Prefixes of an Array / README_EN

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

2640. Find the Score of All Prefixes of an Array

中文文档

Description

We define the conversion array conver of an array arr as follows:

  • conver[i] = arr[i] + max(arr[0..i]) where max(arr[0..i]) is the maximum value of arr[j] over 0 <= j <= i.

We also define the score of an array arr as the sum of the values of the conversion array of arr.

Given a 0-indexed integer array nums of length n, return _an array _ans_ of length _n_ where _ans[i]_ is the score of the prefix_ nums[0..i].

 

Example 1:

Input: nums = [2,3,7,5,10]
Output: [4,10,24,36,56]
Explanation: 
For the prefix [2], the conversion array is [4] hence the score is 4
For the prefix [2, 3], the conversion array is [4, 6] hence the score is 10
For the prefix [2, 3, 7], the conversion array is [4, 6, 14] hence the score is 24
For the prefix [2, 3, 7, 5], the conversion array is [4, 6, 14, 12] hence the score is 36
For the prefix [2, 3, 7, 5, 10], the conversion array is [4, 6, 14, 12, 20] hence the score is 56

Example 2:

Input: nums = [1,1,2,4,8,16]
Output: [2,4,8,16,32,64]
Explanation: 
For the prefix [1], the conversion array is [2] hence the score is 2
For the prefix [1, 1], the conversion array is [2, 2] hence the score is 4
For the prefix [1, 1, 2], the conversion array is [2, 2, 4] hence the score is 8
For the prefix [1, 1, 2, 4], the conversion array is [2, 2, 4, 8] hence the score is 16
For the prefix [1, 1, 2, 4, 8], the conversion array is [2, 2, 4, 8, 16] hence the score is 32
For the prefix [1, 1, 2, 4, 8, 16], the conversion array is [2, 2, 4, 8, 16, 32] hence the score is 64

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109

Solutions

Solution 1

class Solution:
  def findPrefixScore(self, nums: List[int]) -> List[int]:
    n = len(nums)
    ans = [0] * n
    mx = 0
    for i, x in enumerate(nums):
      mx = max(mx, x)
      ans[i] = x + mx + (0 if i == 0 else ans[i - 1])
    return ans
class Solution {
  public long[] findPrefixScore(int[] nums) {
    int n = nums.length;
    long[] ans = new long[n];
    int mx = 0;
    for (int i = 0; i < n; ++i) {
      mx = Math.max(mx, nums[i]);
      ans[i] = nums[i] + mx + (i == 0 ? 0 : ans[i - 1]);
    }
    return ans;
  }
}
class Solution {
public:
  vector<long long> findPrefixScore(vector<int>& nums) {
    int n = nums.size();
    vector<long long> ans(n);
    int mx = 0;
    for (int i = 0; i < n; ++i) {
      mx = max(mx, nums[i]);
      ans[i] = nums[i] + mx + (i == 0 ? 0 : ans[i - 1]);
    }
    return ans;
  }
};
func findPrefixScore(nums []int) []int64 {
  n := len(nums)
  ans := make([]int64, n)
  mx := 0
  for i, x := range nums {
    mx = max(mx, x)
    ans[i] = int64(x + mx)
    if i > 0 {
      ans[i] += ans[i-1]
    }
  }
  return ans
}
function findPrefixScore(nums: number[]): number[] {
  const n = nums.length;
  const ans: number[] = new Array(n);
  let mx: number = 0;
  for (let i = 0; i < n; ++i) {
    mx = Math.max(mx, nums[i]);
    ans[i] = nums[i] + mx + (i === 0 ? 0 : ans[i - 1]);
  }
  return ans;
}

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

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

发布评论

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