Skip to content

Commit ccf0ff6

Browse files
1749_Max_Absolute_Sum_of_Any_Subarray.java
1 parent 5a6670a commit ccf0ff6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Problem Number: 1749
2+
3+
// Maximum Absolute Sum of Any SubArray.
4+
5+
class Solution {
6+
public:
7+
int maxAbsoluteSum(vector<int>& nums) {
8+
int maxSum = 0;
9+
int minSum = 0;
10+
int currMax = 0;
11+
int currMin = 0;
12+
13+
for (int n : nums) {
14+
currMax = max(0, currMax + n);
15+
currMin = min(0, currMin + n);
16+
17+
maxSum = max(maxSum, currMax);
18+
minSum = min(minSum, currMin);
19+
}
20+
21+
return max(maxSum, -minSum);
22+
23+
}
24+
};

0 commit comments

Comments
 (0)