当前位置:网站首页>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
}
}
边栏推荐
- LeetCode每日一题(985. Sum of Even Numbers After Queries)
- PolyWorks script development learning notes (I) - script development environment
- PolyWorks script development learning notes (III) -treeview advanced operation
- 一款开源的Markdown转富文本编辑器的实现原理剖析
- Patent inquiry website
- Overview of database system
- [kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
- Crawler career from scratch (IV): climb the bullet curtain of station B through API
- WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip
- Make the most basic root file system of Jetson nano and mount NFS file system on the server
猜你喜欢
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 2 --blinker_ Hello_ WiFi (lighting technology - Mobile App control routine)
Hudi学习笔记(三) 核心概念剖析
Idea uses the MVN command to package and report an error, which is not available
About the configuration of vs2008+rade CATIA v5r22
Spark cluster installation and deployment
Directory and switching operation in file system
Using Hudi in idea
CATIA automation object architecture - detailed explanation of application objects (III) systemservice
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
随机推荐
Flink学习笔记(九)状态编程
Notes on numerical analysis (II): numerical solution of linear equations
Spark structured stream writing Hudi practice
Filter comments to filter out uncommented and default values
What do software test engineers do? Pass the technology to test whether there are loopholes in the software program
Hudi 快速体验使用(含操作详细步骤及截图)
The idea of compiling VBA Encyclopedia
Common software open source protocols
LeetCode每日一题(931. Minimum Falling Path Sum)
Windows安装Redis详细步骤
Uncle Wang's blog directory [constantly updating]
Jenkins learning (I) -- Jenkins installation
软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
Principles of computer composition - cache, connection mapping, learning experience
Solve editor MD uploads pictures and cannot get the picture address
Banner - Summary of closed group meeting
Analysis of the implementation principle of an open source markdown to rich text editor
QT qstring:: number apply base conversion
Go language - Reflection
Apply for domain name binding IP to open port 80 record