【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/sum-of-even-numbers-after-queries/
题目描述
We have an array A
of integers, and an array queries of queries.
For the i
-th query val = queries[i][0]
, index = queries[i][1]
, we add val to A[index]
. Then, the answer to the i-th
query is the sum of the even values of A
.
(Here, the given index = queries[i][1]
is a 0-based index, and each query permanently modifies the array A.)
Return the answer to all queries. Your answer array should have answer[i]
as the answer to the i-th query.
Example 1:
Input: A = [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 A[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.
After adding -3 to A[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.
After adding -4 to A[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.
After adding 2 to A[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.
Note:
- 1 <= A.length <= 10000
- -10000 <= A[i] <= 10000
- 1 <= queries.length <= 10000
- -10000 <= queries[i][0] <= 10000
- 0 <= queriesi < A.length
题目大意
给出了原始的数组,然后给出了一串查询步骤,每次查询的时候,都会在指定位置queriesi加上queries[i][0],在每次操作完毕之后,把所有数值是偶数的数字求和保存起来。求经过一系列的查询之后,生成的偶数之和序列是多少。
解题方法
暴力
首先,我们可以按照题目描述使用暴力解法。即每次查询之后都去遍历一次,计算偶数之和,保存起来。
设A的长度是M,queries的次数是M,那么时间复杂度是O(N*M),竟然也通过了。C++用时3000ms,代码如下。
class Solution {
public:
vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) {
vector<int> res;
for (auto q : queries) {
A[q[1]] += q[0];
int sum = 0;
for (int a : A) {
if (a % 2 == 0) {
sum += a;
}
}
res.push_back(sum);
}
return res;
}
};
找规律
上面的暴力解法显然不够优美。根据@votrubac的解法,我们可以先求出所有偶数之和,然后对于每次查询的时候,如果A[index]是偶数,那么就把这个值减去,然后把查询要添加的数值val加到A[index]上。如果加完的结果是偶数的话,需要把该结果加到sum上。
怎么证明?
首先,我们求出了所有偶数的和。
然后每次查询更改一个数字,有四种更改方式:
- 偶 ==> 奇
- 偶 ==> 偶
- 奇 ==> 奇
- 奇 ==> 偶
所以,如果我们要求在查询之后的偶数和,可以在初始化的偶数和的基础上,先减去在查询之前是偶数的(因为这些偶数已经计算到和里面了,即将变化走了),然后查询是当前的数字进行了变化,然后再加上变化之后是偶数的(因为这些偶数是新变化出来的,需要加到偶数和里面)。这样就求得了新的所有偶数的和。
C++代码如下:
class Solution {
public:
vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) {
vector<int> sums;
int cursum = 0;
for (int a : A) {
if (a % 2 == 0) {
cursum += a;
}
}
for (auto q : queries) {
if (A[q[1]] % 2 == 0) {
cursum -= A[q[1]];
}
A[q[1]] += q[0];
if (A[q[1]] % 2 == 0) {
cursum += A[q[1]];
}
sums.push_back(cursum);
}
return sums;
}
};
日期
2019 年 2 月 19 日 —— 重拾状态
【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)的更多相关文章
- LeetCode 985 Sum of Even Numbers After Queries 解题报告
题目要求 We have an array A of integers, and an array queries of queries. For the i-th query val = queri ...
- #Leetcode# 985. Sum of Even Numbers After Queries
https://leetcode.com/problems/sum-of-even-numbers-after-queries/ We have an array A of integers, and ...
- 【LEETCODE】47、985. Sum of Even Numbers After Queries
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】985. Sum of Even Numbers After Queries
problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...
- [Solution] 985. Sum of Even Numbers After Queries
Difficulty: Easy Question We have an array A of integers, and an array queries of queries. For the i ...
- 【leetcode】985. Sum of Even Numbers After Queries
题目如下: We have an array A of integers, and an array queries of queries. For the i-th query val = quer ...
- 985. Sum of Even Numbers After Queries
We have an array A of integers, and an array queries of queries. For the i-th query val = queries[i] ...
- LC 985. Sum of Even Numbers After Queries
We have an array A of integers, and an array queries of queries. For the i-th query val = queries[i] ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
随机推荐
- Linux内网时钟同步问题(ntp和chrony)
我们都知道时钟同步可以使用外网服务器,在内网内不能连接外网的时候也需要时钟同步,那怎么进行呢? 选择内网的一台稳定的服务器作为时钟源,然后让其他机器都来同步这台机器即可. 注:其实ntp服务和chro ...
- 前端4 — jQuery — 更新完毕
1.下载jQuery 网址:Download jQuery | jQuery 最好下载最新版的,因为有什么bug问题,最新版的都会有,所以学技术就用最新版的,实际开发用的时候就要讲究了 2.开始用j ...
- MyBatis 如何实现流式查询
基本概念 流式查询指的是查询成功后不是返回一个集合而是返回一个迭代器,应用每次从迭代器取一条查询结果.流式查询的好处是能够降低内存使用. 如果没有流式查询,我们想要从数据库取 1000 万条记录而又没 ...
- fastJson序列化
在pojo实体中有map<String,Object>的属性,有个key是user它存储在数据库中是用户的id数组,而在aop里会对这个属性做用户详细信息查询并重新put给user.在做J ...
- EasyExcel读写Excel
使用过 poi 的开发同学可能都有此体会,每次都要写一坨代码,最后的代码如下面一样: 这样的代码是不是又臭又长?当字段数量多的时候,一不小心还容易写错.阿粉还记得当初使用 poi 导出一个二十多字段的 ...
- centos7 自动同步时间
rm -rf /etc/localtime ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime vim /etc/sysconfig/cloc ...
- SQL模糊查询语句和Escape转义字符
通配符描述示例%包含零个或更多字符的任意字符串.WHERE title LIKE '%computer%' 将查找处于书名任意位置的包含单词 computer 的所有书名._(下划线)任何单个字符.W ...
- LVS nat模型+dr模型
nat模型 在 rs1 和 rs2 安装httpd 并配置测试页,启动 [root@rs1 ~]# yum install httpd -y[root@rs1 ~]# echo "thi ...
- my39_InnoDB锁机制之Gap Lock、Next-Key Lock、Record Lock解析
MySQL InnoDB支持三种行锁定方式: 行锁(Record Lock):锁直接加在索引记录上面,锁住的是key. 间隙锁(Gap Lock): 锁定索引记录间隙,确保索引记录的间隙不变.间隙锁是 ...
- MySQL批量数据脚本示例
一.建表 # 新建库 create database bigData; use bigData; #1 建表dept CREATE TABLE dept( id INT UNSIGNED PRIMAR ...