作者: 负雪明烛
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. 1 <= A.length <= 10000
  2. -10000 <= A[i] <= 10000
  3. 1 <= queries.length <= 10000
  4. -10000 <= queries[i][0] <= 10000
  5. 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++)的更多相关文章

  1. 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 ...

  2. #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 ...

  3. 【LEETCODE】47、985. Sum of Even Numbers After Queries

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  4. 【Leetcode_easy】985. Sum of Even Numbers After Queries

    problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...

  5. [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 ...

  6. 【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 ...

  7. 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] ...

  8. 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] ...

  9. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

随机推荐

  1. 1005.K次取反后最大化的数组和

    1005.K次取反后最大化的数组和 目录 1005.K次取反后最大化的数组和 题目 题解 排序+维护最小值min 题目 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个索引 i 并将 ...

  2. 学习Vue源码前的几项必要储备(一)

    从接下来的一段时间里,Mg要进行阅读源码的工作.再阅读源码前,梳理一下准备工作. 7项重要储备 Flow 基本语法 发布/订阅模式 ES6+ 语法 原型链.闭包 函数柯里化 event loop 1. ...

  3. Linux学习 - 文本编辑器Vim

    一.Vim工作模式 二.命令 插入 a 光标后插入 A 光标所在行尾插入 i 光标前插入 I 光标所在行首插入 o 光标下插入新行 O 光标上插入新行   删除 x 删除光标处字符 nx 删除光标处后 ...

  4. Mysql的索引调优详解:如何去创建索引以及避免索引失效

    在正式介绍Mysql调优之前,先补充mysql的两种引擎 mysql逻辑分层 InnoDB:事务优先(适合高并发操作,行锁) MyISAM:性能优先(表锁) 查看使用的引擎: show variabl ...

  5. hadoop Sort排序

    1 public int getPartition(IntWritable key,IntWritable value,int numPartitions){ 2 int Maxnumber = 12 ...

  6. layui 弹窗中 分页展示table

    1. 需求:点击查看更多,展示该类别 所有数据,并分页 2. 参考文档: (1)https://www.jianshu.com/p/40da11ebae66 (2) https://blog.csdn ...

  7. js 时间戳转换为年月日时分秒的格式

    <script type="text/javascript"> var strDate = ''; $(function(){ // 获取时间戳 var nowDate ...

  8. Properties类继承HashTable类,一般用来给程序配置属性文件。

    package com.itcast.demo04.Prop;import jdk.internal.util.xml.impl.ReaderUTF8;import sun.nio.cs.UTF_32 ...

  9. 第43篇-JNI引用的管理(2)

    之前我们已经介绍了JNIHandleBlock,但是没有具体介绍JNIHandleBlock中存储的句柄,这一篇我们将详细介绍对这些句柄的操作. JNI句柄分为两种,全局和局部对象引用: (1)大部分 ...

  10. CPU的负载

    目录 一.简介 二.合理的负载 一.简介 使用top或者uptime命令可以看到cpu平均负载,1,5,15分钟 平均负载包括以下几个部分: 正在运行的进程.正在使用cpu做计算的进程,ps看到R 也 ...