leetcode

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

1652.cpp (521B)


      1 class Solution {
      2   public:
      3     vector<int> decrypt(vector<int> &code, int k) {
      4         const int n = size(code);
      5         vector<int> res(n, 0);
      6 
      7         if (k == 0) return res;
      8 
      9         auto [start, end] = k >= 0 ? tuple(1, k) : tuple(n + k, n - 1);
     10         int sum = 0;
     11 
     12         for (int i = start; i <= end; i++)
     13             sum += code[i];
     14         for (int i = 0; i < n; i++) {
     15             res[i] = sum;
     16             sum -= code[start++ % n];
     17             sum += code[++end % n];
     18         }
     19 
     20         return res;
     21     }
     22 };