|
| 1 | +// Source : https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-02-12 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * You are given an integer array nums. The absolute sum of a subarray [numsl, numsl+1, ..., numsr-1, |
| 8 | + * numsr] is abs(numsl + numsl+1 + ... + numsr-1 + numsr). |
| 9 | + * |
| 10 | + * Return the maximum absolute sum of any (possibly empty) subarray of nums. |
| 11 | + * |
| 12 | + * Note that abs(x) is defined as follows: |
| 13 | + * |
| 14 | + * If x is a negative integer, then abs(x) = -x. |
| 15 | + * If x is a non-negative integer, then abs(x) = x. |
| 16 | + * |
| 17 | + * Example 1: |
| 18 | + * |
| 19 | + * Input: nums = [1,-3,2,3,-4] |
| 20 | + * Output: 5 |
| 21 | + * Explanation: The subarray [2,3] has absolute sum = abs(2+3) = abs(5) = 5. |
| 22 | + * |
| 23 | + * Example 2: |
| 24 | + * |
| 25 | + * Input: nums = [2,-5,1,-4,3,-2] |
| 26 | + * Output: 8 |
| 27 | + * Explanation: The subarray [-5,1,-4] has absolute sum = abs(-5+1-4) = abs(-8) = 8. |
| 28 | + * |
| 29 | + * Constraints: |
| 30 | + * |
| 31 | + * 1 <= nums.length <= 105 |
| 32 | + * -104 <= nums[i] <= 104 |
| 33 | + ******************************************************************************************************/ |
| 34 | + |
| 35 | +class Solution { |
| 36 | +public: |
| 37 | + int maxAbsoluteSum(vector<int>& nums) { |
| 38 | + return maxAbsoluteSum02(nums); //56ms |
| 39 | + return maxAbsoluteSum01(nums); //56ms |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + int maxAbsoluteSum01(vector<int>& nums) { |
| 45 | + return std::max(maxSumArray(nums), std::abs(minSumArray(nums)) ); |
| 46 | + } |
| 47 | + |
| 48 | + //https://en.wikipedia.org/wiki/Maximum_subarray_problem |
| 49 | + int maxSumArray(vector<int>& nums) { |
| 50 | + int max = nums[0]; |
| 51 | + int sum = nums[0]; |
| 52 | + for (int i = 1; i < nums.size(); i++) { |
| 53 | + if (sum < 0 ) sum = nums[i]; |
| 54 | + else sum += nums[i]; |
| 55 | + if (max < sum) max = sum; |
| 56 | + } |
| 57 | + return max; |
| 58 | + } |
| 59 | + |
| 60 | + int minSumArray(vector<int>& nums) { |
| 61 | + int min = nums[0]; |
| 62 | + int sum = nums[0]; |
| 63 | + for (int i = 1; i < nums.size(); i++) { |
| 64 | + if (sum > 0 ) sum = nums[i]; |
| 65 | + else sum += nums[i]; |
| 66 | + if (min > sum) min = sum; |
| 67 | + } |
| 68 | + return min; |
| 69 | + } |
| 70 | + |
| 71 | + // Becasue maxSumArray() & minSumArray() are two similar, |
| 72 | + // we can merge them together to save one loop |
| 73 | + int maxAbsoluteSum02(vector<int>& nums) { |
| 74 | + int max = nums[0]; |
| 75 | + int max_sum = nums[0]; |
| 76 | + |
| 77 | + int min = nums[0]; |
| 78 | + int min_sum = nums[0]; |
| 79 | + |
| 80 | + for (int i = 1; i < nums.size(); i++) { |
| 81 | + |
| 82 | + if (max_sum < 0 ) max_sum = nums[i]; |
| 83 | + else max_sum += nums[i]; |
| 84 | + if (max < max_sum) max = max_sum; |
| 85 | + |
| 86 | + |
| 87 | + if (min_sum > 0 ) min_sum = nums[i]; |
| 88 | + else min_sum += nums[i]; |
| 89 | + if (min > min_sum) min = min_sum; |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + return std::max(max, abs(min)); |
| 94 | + } |
| 95 | + |
| 96 | +}; |
0 commit comments