leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0985.cpp (678B)


      1 
      2 #pragma GCC optimize("fast")
      3 static auto _ = []() {
      4     ios_base::sync_with_stdio(false);
      5     cin.tie(nullptr);
      6     cout.tie(nullptr);
      7     return 0;
      8 }();
      9 
     10 class Solution {
     11   public:
     12     vector<int> sumEvenAfterQueries(vector<int> &nums, const vector<vector<int>> &queries) {
     13         vector<int> res;
     14         res.reserve(queries.size());
     15         int sum = 0;
     16         for (int n : nums)
     17             if (n % 2 == 0) sum += n;
     18         for (const auto q : queries) {
     19             if (nums[q[1]] % 2 == 0) sum -= nums[q[1]];
     20             nums[q[1]] += q[0];
     21             if (nums[q[1]] % 2 == 0) sum += nums[q[1]];
     22             res.push_back(sum);
     23         }
     24         return res;
     25     }
     26 };