当前位置:网站首页>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
}
}
边栏推荐
- Jenkins learning (I) -- Jenkins installation
- npm install安装依赖包报错解决方法
- LeetCode每日一题(745. Prefix and Suffix Search)
- 图像修复方法研究综述----论文笔记
- Go language - IO project
- LeetCode每日一题(931. Minimum Falling Path Sum)
- PolyWorks script development learning notes (4) - data import and alignment using file import
- Run flash demo on ECS
- Jestson nano downloads updated kernel and DTB from TFTP server
- Flink学习笔记(十)Flink容错机制
猜你喜欢

IDEA 中使用 Hudi

Go language - JSON processing

Detailed steps of windows installation redis

NPM install installation dependency package error reporting solution

Flink-CDC实践(含实操步骤与截图)

Win10 install elk

Apply for domain name binding IP to open port 80 record

Hudi 数据管理和存储概述
![[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
![[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
随机推荐
PolyWorks script development learning notes (II) -treeview basic operations
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
QT qstring:: number apply base conversion
LeetCode每日一题(2305. Fair Distribution of Cookies)
Directory and switching operation in file system
Usage of pandas to obtain MySQL data
Run flash demo on ECS
基于opencv实现桌面图标识别
Win10 quick screenshot
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
Using Hudi in idea
Crawler career from scratch (V): detailed explanation of re regular expression
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
Spark cluster installation and deployment
PolyWorks script development learning notes (4) - data import and alignment using file import
Win10 install elk
The idea of compiling VBA Encyclopedia
Navicat, MySQL export Er graph, er graph
Flink学习笔记(八)多流转换
Construction of simple database learning environment