返回介绍

solution / 0900-0999 / 0985.Sum of Even Numbers After Queries / README_EN

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

985. Sum of Even Numbers After Queries

中文文档

Description

You are given an integer array nums and an array queries where queries[i] = [vali, indexi].

For each query i, first, apply nums[indexi] = nums[indexi] + vali, then print the sum of the even values of nums.

Return _an integer array _answer_ where _answer[i]_ is the answer to the _ith_ query_.

 

Example 1:

Input: nums = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
Output: [8,6,2,4]
Explanation: At the beginning, the array is [1,2,3,4].
After adding 1 to nums[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.
After adding -3 to nums[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.
After adding -4 to nums[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.
After adding 2 to nums[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.

Example 2:

Input: nums = [1], queries = [[4,0]]
Output: [0]

 

Constraints:

  • 1 <= nums.length <= 104
  • -104 <= nums[i] <= 104
  • 1 <= queries.length <= 104
  • -104 <= vali <= 104
  • 0 <= indexi < nums.length

Solutions

Solution 1

class Solution:
  def sumEvenAfterQueries(
    self, nums: List[int], queries: List[List[int]]
  ) -> List[int]:
    s = sum(x for x in nums if x % 2 == 0)
    ans = []
    for v, i in queries:
      if nums[i] % 2 == 0:
        s -= nums[i]
      nums[i] += v
      if nums[i] % 2 == 0:
        s += nums[i]
      ans.append(s)
    return ans
class Solution {
  public int[] sumEvenAfterQueries(int[] nums, int[][] queries) {
    int s = 0;
    for (int x : nums) {
      if (x % 2 == 0) {
        s += x;
      }
    }
    int m = queries.length;
    int[] ans = new int[m];
    int k = 0;
    for (var q : queries) {
      int v = q[0], i = q[1];
      if (nums[i] % 2 == 0) {
        s -= nums[i];
      }
      nums[i] += v;
      if (nums[i] % 2 == 0) {
        s += nums[i];
      }
      ans[k++] = s;
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> sumEvenAfterQueries(vector<int>& nums, vector<vector<int>>& queries) {
    int s = 0;
    for (int x : nums) {
      if (x % 2 == 0) {
        s += x;
      }
    }
    vector<int> ans;
    for (auto& q : queries) {
      int v = q[0], i = q[1];
      if (nums[i] % 2 == 0) {
        s -= nums[i];
      }
      nums[i] += v;
      if (nums[i] % 2 == 0) {
        s += nums[i];
      }
      ans.push_back(s);
    }
    return ans;
  }
};
func sumEvenAfterQueries(nums []int, queries [][]int) (ans []int) {
  s := 0
  for _, x := range nums {
    if x%2 == 0 {
      s += x
    }
  }
  for _, q := range queries {
    v, i := q[0], q[1]
    if nums[i]%2 == 0 {
      s -= nums[i]
    }
    nums[i] += v
    if nums[i]%2 == 0 {
      s += nums[i]
    }
    ans = append(ans, s)
  }
  return
}
function sumEvenAfterQueries(nums: number[], queries: number[][]): number[] {
  let s = 0;
  for (const x of nums) {
    if (x % 2 === 0) {
      s += x;
    }
  }
  const ans: number[] = [];
  for (const [v, i] of queries) {
    if (nums[i] % 2 === 0) {
      s -= nums[i];
    }
    nums[i] += v;
    if (nums[i] % 2 === 0) {
      s += nums[i];
    }
    ans.push(s);
  }
  return ans;
}
/**
 * @param {number[]} nums
 * @param {number[][]} queries
 * @return {number[]}
 */
var sumEvenAfterQueries = function (nums, queries) {
  let s = 0;
  for (const x of nums) {
    if (x % 2 === 0) {
      s += x;
    }
  }
  const ans = [];
  for (const [v, i] of queries) {
    if (nums[i] % 2 === 0) {
      s -= nums[i];
    }
    nums[i] += v;
    if (nums[i] % 2 === 0) {
      s += nums[i];
    }
    ans.push(s);
  }
  return ans;
};

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

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

发布评论

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