当前位置:网站首页>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
}
}
边栏推荐
- Common formulas of probability theory
- Spark 概述
- Hudi learning notes (III) analysis of core concepts
- Filter comments to filter out uncommented and default values
- Installation and uninstallation of pyenv
- CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers
- PolyWorks script development learning notes (I) - script development environment
- Apply for domain name binding IP to open port 80 record
- 用Redis实现分布式锁
- Redis learning (I)
猜你喜欢

Construction of simple database learning environment

Utilisation de hudi dans idea

Build a solo blog from scratch

LeetCode每日一题(2090. K Radius Subarray Averages)
![[CSDN] C1 training problem analysis_ Part III_ JS Foundation](/img/b2/68d53ad09688f7fc922ac65e104f15.png)
[CSDN] C1 training problem analysis_ Part III_ JS Foundation

解决Editor.md上传图片获取不到图片地址问题

Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
![[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?](/img/fc/5c71e6457b836be04583365edbe08d.png)
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?

Hudi integrated spark data analysis example (including code flow and test results)

Windows安装Redis详细步骤
随机推荐
QT qstring:: number apply base conversion
Jenkins learning (II) -- setting up Chinese
Derivation of Fourier transform
Liteide is easy to use
Uncle Wang's blog directory [constantly updating]
WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
Win10安装ELK
Hudi quick experience (including detailed operation steps and screenshots)
ERROR: certificate common name “*.” doesn’t match requested ho
基于opencv实现桌面图标识别
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
Alibaba cloud notes for the first time
IDEA 中使用 Hudi
Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
[untitled] use of cmake
Vscode编辑器右键没有Open In Default Browser选项
Solve the problem of disordered code in vscode development, output Chinese and open source code
Flink-CDC实践(含实操步骤与截图)
Long类型的相等判断
Go language - Reflection