int prefixSum = 0; int ans = 0; for (int i = 0; i < nums.size(); i ++) { prefixSum += nums[i]; if (prefixSumCount.count(prefixSum-k)) { ans += prefixSumCount[prefixSum-k]; } prefixSumCount[prefixSum] ++; } return ans; } };
classSolution { public: vector<int> maxSlidingWindow(vector<int>& nums, int k){ int n = nums.size();
deque<int> candidates;
for (int i = 0; i < n && i < k; i ++) { while (!candidates.empty() && nums[i] >= nums[candidates.back()]) { candidates.pop_back(); } candidates.push_back(i); } vector<int> ans; ans.push_back(nums[candidates.front()]);
for (int r = k; r < n; r ++) { int l = r - k + 1;
while (!candidates.empty() && candidates.front() < l) { candidates.pop_front(); }
auto ch2idx = [](char c) -> int { return c >= 'a' ? 26 + c - 'a' : c - 'A'; };
for (int i = 0; i < tLen; i ++) { int idx = ch2idx(t[i]); tCount[idx] ++; }
int l = 0; int minL = -1, minLen = sLen; for (int r = 0; r < sLen; r ++) { sCount[ch2idx(s[r])] ++; while (contain(sCount, tCount) && l <= r) { if (minLen >= (r - l + 1)) { minL = l; minLen = r - l + 1; } sCount[ch2idx(s[l])] --; l ++; } }