leetcode

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

2090.cpp (544B)


      1 class Solution {
      2   public:
      3     vector<int> getAverages(const vector<int> &nums, int k) {
      4         int n = nums.size();
      5         if (n <= 2 * k) return vector<int>(n, -1);
      6 
      7         vector<int> res(n);
      8         long long sum = 0;
      9         for (int i = 0; i < k; i++) {
     10             sum += nums[i] + nums[i + k];
     11             res[i] = res[n - k + i] = -1;
     12         }
     13 
     14         for (int i = k; i < n - k; i++) {
     15             sum += nums[i + k];
     16             res[i] = sum / (2 * k + 1);
     17             sum -= nums[i - k];
     18         }
     19 
     20         return res;
     21     }
     22 };