leetcode

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

3080.cpp (902B)


      1 class Solution {
      2   public:
      3     vector<long long> unmarkedSumArray(vector<int> &nums, const vector<vector<int>> &queries) const {
      4         const int n = size(nums), m = size(queries);
      5         long long sum = accumulate(begin(nums), end(nums), 0ll);
      6 
      7         const auto mark = [&](int idx) {
      8             if (nums[idx] < 0) return false;
      9             sum += nums[idx] = -nums[idx];
     10             return true;
     11         };
     12 
     13         vector<pair<int, int>> pairs(n);
     14         for (int i = 0; i < n; i++)
     15             pairs[i] = {nums[i], i};
     16         sort(begin(pairs), end(pairs));
     17 
     18         int crnt = 0;
     19         vector<long long> res(m);
     20         for (int i = 0; i < m; i++) {
     21             mark(queries[i][0]);
     22 
     23             int k = queries[i][1];
     24             while (crnt < n && k > 0) {
     25                 k -= mark(pairs[crnt++].second);
     26             }
     27 
     28             res[i] = sum;
     29         }
     30 
     31         return res;
     32     }
     33 };