The score of an array is defined as the product of its sum and its length.
For example, the score of [1, 2, 3, 4, 5] is (1 + 2 + 3 + 4 + 5) * 5 = 75.
Given a positive integer array nums and an integer k, return the number of non-empty subarrays of nums whose score is strictly less than k.
A subarray is a contiguous sequence of elements within an array.
two pointer solution
Time : O(n)
Space : O(n)
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: longlongcountSubarrays(vector<int>& A, longlong k){ vector<longlong> psum{0}; longlong res = 0, l = 0, n = A.size(); for(int i = 0; i < n; i++) { psum.push_back(psum.back() + A[i]); while((psum.back() - psum[l]) * (i - l + 1) >= k) l++; res += (i - l + 1); } return res; } };