1749.cpp (614B)
1 class Solution { 2 public: 3 int maxAbsoluteSum(const vector<int> &nums) const { 4 int mini = 0, maxi = 0, res = 0; 5 for (const int n : nums) { 6 maxi = max(0, maxi + n); 7 mini = min(0, mini + n); 8 res = max({res, maxi, -mini}); 9 } 10 return res; 11 } 12 }; 13 14 class Solution { 15 public: 16 int maxAbsoluteSum(const vector<int> &nums) const { 17 int mini = 0, maxi = 0, sum = 0; 18 for (const int n : nums) { 19 sum += n; 20 mini = min(mini, sum); 21 maxi = max(maxi, sum); 22 } 23 return maxi - mini; 24 } 25 };