Skip to content

Commit a9630a2

Browse files
committed
Add solution #1749
1 parent 511f16a commit a9630a2

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@
580580
1726|[Tuple with Same Product](./1726-tuple-with-same-product.js)|Medium|
581581
1732|[Find the Highest Altitude](./1732-find-the-highest-altitude.js)|Easy|
582582
1748|[Sum of Unique Elements](./1748-sum-of-unique-elements.js)|Easy|
583+
1749|[Maximum Absolute Sum of Any Subarray](./1749-maximum-absolute-sum-of-any-subarray.js)|Medium|
583584
1752|[Check if Array Is Sorted and Rotated](./1752-check-if-array-is-sorted-and-rotated.js)|Easy|
584585
1764|[Form Array by Concatenating Subarrays of Another Array](./1764-form-array-by-concatenating-subarrays-of-another-array.js)|Medium|
585586
1765|[Map of Highest Peak](./1765-map-of-highest-peak.js)|Medium|
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 1749. Maximum Absolute Sum of Any Subarray
3+
* https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array nums. The absolute sum of a subarray
7+
* [numsl, numsl+1, ..., numsr-1, numsr] is abs(numsl + numsl+1 + ... + numsr-1 + numsr).
8+
*
9+
* Return the maximum absolute sum of any (possibly empty) subarray of nums.
10+
*
11+
* Note that abs(x) is defined as follows:
12+
* - If x is a negative integer, then abs(x) = -x.
13+
* - If x is a non-negative integer, then abs(x) = x.
14+
*/
15+
16+
/**
17+
* @param {number[]} nums
18+
* @return {number}
19+
*/
20+
var maxAbsoluteSum = function(nums) {
21+
let result = 0;
22+
23+
for (let i = 0, min = 0, max = 0; i < nums.length; i++) {
24+
max = Math.max(nums[i], max + nums[i]);
25+
min = Math.min(nums[i], min + nums[i]);
26+
result = Math.max(result, Math.abs(max), Math.abs(min));
27+
}
28+
29+
return result;
30+
};

0 commit comments

Comments
 (0)