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:

  1. Input: A = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
  2. Output: [8,6,2,4]
  3. Explanation:
  4. At the beginning, the array is [1,2,3,4].
  5. After adding 1 to A[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.
  6. After adding -3 to A[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.
  7. After adding -4 to A[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.
  8. 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 <= queries[i][1] < A.length
 Idea 1. To avoid go through the array A every time a query element is visited, saving the even sum is a good idea, plus taking the observation:
even + even -> even
odd + odd -> even
even + odd -> odd
Time complexity: O(N + Q), N = A.length, Q = queries.length
Space complexity: O(1) 
  1. class Solution {
  2. public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
  3. int sum = 0;
  4. int[] result = new int[queries.length];
  5. for(int a: A) {
  6. if((a&1) == 0) {
  7. sum += a;
  8. }
  9. }
  10.  
  11. for(int i = 0; i < queries.length; ++i) {
  12. int[] query = queries[i];
  13. int index = query[1];
  14. int val = query[0];
  15. if((A[index]&1) == 0) {
  16. if((val&1) == 0) {
  17. sum+= val;
  18. }
  19. else {
  20. sum -= A[index];
  21. }
  22. }
  23. else {
  24. if((val&1) == 1) {
  25. sum += val + A[index];
  26. }
  27. }
  28. A[index] += val;
  29. result[i] = sum;
  30. }
  31.  
  32. return result;
  33. }
  34. }

看到官方解法才感觉上面的解法笨,好好看题,只有even value matters, new value replaced by one one, if old one is even, deduct it, if new value is even, add it, simple as it is, no need to check all the combinations of even/odd cases.

  1. class Solution {
  2. public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
  3. int sum = 0;
  4. int[] result = new int[queries.length];
  5. for(int a: A) {
  6. if((a&1) == 0) {
  7. sum += a;
  8. }
  9. }
  10.  
  11. for(int i = 0; i < queries.length; ++i) {
  12. int[] query = queries[i];
  13. int index = query[1];
  14. int val = query[0];
  15.  
  16. if((A[index] & 1) == 0) {
  17. sum -= A[index];
  18. }
  19. A[index] += val;
  20. if((A[index] & 1) == 0) {
  21. sum += A[index];
  22. }
  23. result[i] = sum;
  24. }
  25.  
  26. return result;
  27. }
  28. }

Sum of Even Numbers After Queries LT985的更多相关文章

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

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

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

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

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

  4. [Swift]LeetCode985. 查询后的偶数和 | 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] ...

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

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

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

  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.985-查询后偶数的总和(Sum of Even Numbers After Queries)

    这是悦乐书的第370次更新,第398篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第232题(顺位题号是985).有一个整数数组A和一个查询数组queries. 对于第i ...

随机推荐

  1. <ROS> 机器人描述--URDF和XACRO

    文章转自 https://blog.csdn.net/sunbibei/article/details/52297524 特此鸣谢原创作者的辛勤付出 1 URDF 文件 1.1 link和joint ...

  2. Win10下JDK环境变量的设置

    1.找到jdk正确的安装路径 2.打开环境变量设置 打开"资管管理器"后,右击"此电脑",点击"属性" 然后点击"高级系统设置&q ...

  3. mysql大表设计以及优化

    MYSQL千万级数据量的优化方法积累https://m.toutiao.com/group/6583260372269007374/?iid=6583260372269007374 MySQL 千万级 ...

  4. android 获取对权限的选择

    一般是第三方软件拦截,再次提示给用户,确认权限的,如360等.(PS 没有设置权限的app 是会崩溃的  ,而是不是弹出权限确认,因为你都没设置这个权限)看了网上很多,确切说没有一个适合我的. 其实用 ...

  5. linux git 保存用户名和密码

    一.通过文件方式 1.在~/下, touch创建文件 .git-credentials, 用vim编辑此文件,输入内容格式: touch .git-credentials vim .git-crede ...

  6. 基于MNIST数据集使用TensorFlow训练一个没有隐含层的浅层神经网络

    基础 在参考①中我们详细介绍了没有隐含层的神经网络结构,该神经网络只有输入层和输出层,并且输入层和输出层是通过全连接方式进行连接的.具体结构如下: 我们用此网络结构基于MNIST数据集(参考②)进行训 ...

  7. 当一个HTML元素需要添加mouseon、mouseout与click事件,或者mouserenter、mouseleave和click事件时,click事件无法触发

    当一个HTML元素需要添加mouseon.mouseout与click事件,或者mouserenter.mouseleave和click事件时,click事件无法触发 针对上述问题,我遇到的有两种情况 ...

  8. Chrome默认搜索引擎被窜改

    折腾了几个安全软件之后,Chrome浏览器默认搜索引擎被窜改,且无法删除,提示“管理员安装”,另外在插件栏出现了一个unTabs的东西,也无法删除,“受政策保护之类的”.搜索后找到一篇文章“Unabl ...

  9. delete content on the right of cursor, Mac

    delete content on the right of cursor, Mac It's not convenient to press Fn+delete to delete content ...

  10. 高校手机签到系统——第一部分Authority权限系统(上)

    序:今天开始写一个算是我第一个系列的文章——高校手机签到系统.本系统结合我们学校自身的一些特点编写.这是我的毕业设计项目,写在这里算是给最后论文的时候一些点滴的记录.另外也想通过这个系列的文章找到一份 ...