当前位置:网站首页>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
}
}
边栏推荐
- Uncle Wang's blog directory [constantly updating]
- Beego learning - Tencent cloud upload pictures
- 1922. Count Good Numbers
- 基于opencv实现桌面图标识别
- 【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
- Spark cluster installation and deployment
- Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
- 【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
- 全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
- Hudi data management and storage overview
猜你喜欢

Crawler career from scratch (IV): climb the bullet curtain of station B through API

Global KYC service provider advance AI in vivo detection products have passed ISO international safety certification, and the product capability has reached a new level
![[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

Modify idea code
![[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?

【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字

Go language - Reflection

Navicat, MySQL export Er graph, er graph

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

Idea uses the MVN command to package and report an error, which is not available
随机推荐
LeetCode每日一题(2305. Fair Distribution of Cookies)
Construction of simple database learning environment
Win10 install elk
Win10安装ELK
[kotlin learning] classes, objects and interfaces - define class inheritance structure
Directory and switching operation in file system
Vscode Arduino installation Library
Jenkins learning (III) -- setting scheduled tasks
307. Range Sum Query - Mutable
PolyWorks script development learning notes (I) - script development environment
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
Beego learning - Tencent cloud upload pictures
Jetson nano custom boot icon kernel logo CBOOT logo
Hudi learning notes (III) analysis of core concepts
QT qstring:: number apply base conversion
Hudi 数据管理和存储概述
从0开始使用pnpm构建一个Monorepo方式管理的demo
DSP data calculation error
Crawler career from scratch (IV): climb the bullet curtain of station B through API
unbuntu(debian)下TFTP服务器搭建及测试