forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
29 lines (29 loc) · 850 Bytes
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
long long maximumSubarraySum(vector<int>& nums, int k) {
unordered_map<int, long long> p;
p[nums[0]] = 0;
long long s = 0;
const int n = nums.size();
long long ans = LONG_LONG_MIN;
for (int i = 0;; ++i) {
s += nums[i];
auto it = p.find(nums[i] - k);
if (it != p.end()) {
ans = max(ans, s - it->second);
}
it = p.find(nums[i] + k);
if (it != p.end()) {
ans = max(ans, s - it->second);
}
if (i + 1 == n) {
break;
}
it = p.find(nums[i + 1]);
if (it == p.end() || it->second > s) {
p[nums[i + 1]] = s;
}
}
return ans == LONG_LONG_MIN ? 0 : ans;
}
};