Skip to content

LeetCode: 985-Sum of Even Numbers After Queries 解題紀錄

Last Updated on 2022-09-21 by Clay

題目

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

題目會給予一組數值 nums 以及一串查詢 queriesqueries 中的第一個值是要加上去的數值,第二個數值則是要加的 nums 位置。

而我們需要把每次加值後的新 nums,我們需要把當中所有的『偶數』值加總,最後返回一組紀錄了過程中每次改動值後 nums 中的偶數值加總值的陣列。


解題思路

這題的限制設定得很巧妙,使用雙層 for 迴圈的話一定過不了測試(TLE)。

所以最單純的暴力法就是:我們一開始就計算好了 nums 中所有偶數值加總值,接下來再每次改動值時,分別對加總值進行調整,最後才紀錄進要回傳的陣列。

而改動 nums 分成四種狀況:

  • 舊值為偶數、加值為偶數: 把當前加總值再加上加值即可
  • 舊值為偶數、加值為奇數: 把當前加總值減去舊值
  • 舊值為奇數、加值為偶數: 新值仍為奇數,當前加總值不變
  • 舊值為奇數、加值為奇數: 把當前加總值加上新值

這樣就可以在一層 for 迴圈中計算出所有偶數加總值答案。


C++ 範例程式碼

class Solution {
public:
    vector<int> sumEvenAfterQueries(vector<int>& nums, vector<vector<int>>& queries) {
        // Init
        vector<int> results;
        int sum = 0;
        for (auto& n: nums) {
            if (n % 2 == 0) {
                sum += n;
            }
        }
                
        // Add
        for (int i=0; i<queries.size(); ++i) {
            int _add = queries[i][0];
            int _old = nums[queries[i][1]];
            
            nums[queries[i][1]] += queries[i][0];
            int _new = nums[queries[i][1]];
                        
            if (_old % 2 == 0) {
                if (_add % 2 == 0) {
                    sum += _add;
                }
                else {
                    sum -= _old;
                }
            }
            else {
                if (_add % 2 != 0) {
                    sum += _new;
                }
            }
                        
            results.push_back(sum);
        }
        
        return results;
    }
};

Python 範例程式碼

class Solution:
    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:
        # Init
        results = []
        _sum = sum([n for n in nums if n % 2 == 0])
                
        # Add
        for query in queries:
            _add = query[0]
            _old = nums[query[1]]
            nums[query[1]] += _add
            _new = nums[query[1]]
                        
            if _old % 2 == 0:
                if _add % 2 == 0:
                    _sum += _add
                else:
                    _sum -= _old
            else:
                if _add % 2 != 0:
                    _sum += _new
                        
            results.append(_sum)
        
        return results



一定還有更快的做法,但是今天就比較沒空。暴力了事 OWO


References


Read More

Leave a Reply取消回覆

Exit mobile version