{"id":4761,"date":"2019-02-02T22:10:19","date_gmt":"2019-02-03T06:10:19","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4761"},"modified":"2019-02-02T22:30:47","modified_gmt":"2019-02-03T06:30:47","slug":"leetcode-985-sum-of-even-numbers-after-queries","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-985-sum-of-even-numbers-after-queries\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 985. Sum of Even Numbers After Queries"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Problem<\/h2>\n\n\n\n<p>We have an array&nbsp;<code>A<\/code>&nbsp;of integers, and an array&nbsp;<code>queries<\/code>&nbsp;of queries.<\/p>\n\n\n\n<p>For the&nbsp;<code>i<\/code>-th&nbsp;query&nbsp;<code>val =&nbsp;queries[i][0], index&nbsp;= queries[i][1]<\/code>, we add&nbsp;val&nbsp;to&nbsp;<code>A[index]<\/code>.&nbsp; Then, the answer to the&nbsp;<code>i<\/code>-th query is the sum of the even values of&nbsp;<code>A<\/code>.<\/p>\n\n\n\n<p><em>(Here, the given&nbsp;<code>index = queries[i][1]<\/code>&nbsp;is a 0-based index, and each query permanently modifies the array&nbsp;<code>A<\/code>.)<\/em><\/p>\n\n\n\n<p>Return the answer to all queries.&nbsp; Your&nbsp;<code>answer<\/code>&nbsp;array should have&nbsp;<code>answer[i]<\/code>&nbsp;as&nbsp;the answer to the&nbsp;<code>i<\/code>-th query.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>A = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]\n<strong>Output: <\/strong>[8,6,2,4]\n<strong>Explanation: <\/strong>\nAt the beginning, the array is [1,2,3,4].\nAfter adding 1 to A[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.\nAfter adding -3 to A[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.\nAfter adding -4 to A[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.\nAfter adding 2 to A[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= A.length &lt;= 10000<\/code><\/li><li><code>-10000 &lt;= A[i] &lt;= 10000<\/code><\/li><li><code>1 &lt;= queries.length &lt;= 10000<\/code><\/li><li><code>-10000 &lt;= queries[i][0] &lt;= 10000<\/code><\/li><li><code>0 &lt;= queries[i][1] &lt; A.length<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Solution: Simulation<\/h2>\n\n\n\n<p>Time complexity: O(n + |Q|)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, Running time: 100 ms \/ 6.5 MB\nclass Solution {\npublic:\n  vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) {\n    int sum = 0;\n    for (int i = 0; i < A.size(); ++i)\n      if (A[i] % 2 == 0)\n        sum += A[i];\n    vector<int> ans;\n    for (const auto& query : queries) {\n      int& a = A[query[1]];\n      if (a % 2 == 0)\n        sum -= a;\n      a += query[0];\n      if (a % 2 == 0)\n        sum += a;\n      ans.push_back(sum);\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua, running time: 188 ms \/ 16.4MB\nclass Solution:\n  def sumEvenAfterQueries(self, A: 'List[int]', queries: 'List[List[int]]') -> 'List[int]':\n    s = sum(x for x in A if x % 2 == 0)\n    ans = []\n    for val, index in queries:\n      if A[index] % 2 == 0: s -= A[index]\n      A[index] += val\n      if A[index] % 2 == 0: s += A[index]\n      ans.append(s)\n    return ans\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem We have an array&nbsp;A&nbsp;of integers, and an array&nbsp;queries&nbsp;of queries. For the&nbsp;i-th&nbsp;query&nbsp;val =&nbsp;queries[i][0], index&nbsp;= queries[i][1], we add&nbsp;val&nbsp;to&nbsp;A[index].&nbsp; Then, the answer to the&nbsp;i-th query is the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[20,222,179,62],"class_list":["post-4761","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-array","tag-easy","tag-simulation","tag-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4761"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4761\/revisions"}],"predecessor-version":[{"id":4767,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4761\/revisions\/4767"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}