当前位置:网站首页>Leetcode daily question (985. sum of even numbers after queries)
Leetcode daily question (985. sum of even numbers after queries)
2022-07-03 09:33:00 【wangjun861205】
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
consider 4 In this case :
- nums[i] It turned out to be an even number , add val Then even numbers , So the even sum increases val
- nums[i] It turned out to be an even number , add val Then it becomes odd , In this way, the even sum is reduced nums[i]
- nums[i] It turned out to be an odd number , add val Then it becomes even , So the even sum increases nums[i] + val
- nums[i] It turned out to be an odd number , add val Then there are odd numbers , So there is no change in the even sum
impl Solution {
pub fn sum_even_after_queries(mut nums: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<i32> {
let mut sum = nums.iter().filter(|v| *v % 2 == 0).map(|v| *v).sum();
let mut ans = Vec::new();
for q in queries {
let curr = nums[q[1] as usize];
if curr % 2 == 0 {
if (curr + q[0]) % 2 == 0 {
sum += q[0];
} else {
sum -= curr;
}
} else {
if (curr + q[0]) % 2 == 0 {
sum += curr + q[0];
}
}
nums[q[1] as usize] += q[0];
ans.push(sum);
}
ans
}
}
边栏推荐
- Derivation of Fourier transform
- 专利查询网站
- WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip
- Detailed steps of windows installation redis
- NPM install installation dependency package error reporting solution
- Beego learning - JWT realizes user login and registration
- Linxu learning (4) -- Yum and apt commands
- PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
- 307. Range Sum Query - Mutable
- Word segmentation in full-text indexing
猜你喜欢

PolyWorks script development learning notes (4) - data import and alignment using file import

Solve editor MD uploads pictures and cannot get the picture address

Hudi integrated spark data analysis example (including code flow and test results)
![[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu](/img/57/b413a93a456a1872fc19aa825c937a.jpg)
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu

Liteide is easy to use

全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶

LeetCode每日一题(931. Minimum Falling Path Sum)

CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers

Spark overview
![[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis](/img/c6/5f723d9021cf684dcfb662ed3acaec.png)
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
随机推荐
LeetCode每日一题(1162. As Far from Land as Possible)
LeetCode每日一题(2212. Maximum Points in an Archery Competition)
Directory and switching operation in file system
Jestson Nano自定义根文件系统创建(支持NVIDIA图形库的最小根文件系统)
Spark structured stream writing Hudi practice
Windows安装Redis详细步骤
LeetCode每日一题(1024. Video Stitching)
Spark 概述
[CSDN]C1训练题解析_第三部分_JS基础
Spark cluster installation and deployment
Jetson nano custom boot icon kernel logo CBOOT logo
Solve the problem of disordered code in vscode development, output Chinese and open source code
IDEA 中使用 Hudi
Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
Call the contents of Excel cells opened at the same time - button line feed
Jenkins learning (II) -- setting up Chinese
Spark 集群安装与部署
数字身份验证服务商ADVANCE.AI顺利加入深跨协 推进跨境电商行业可持续性发展
Install database -linux-5.7
Temper cattle ranking problem