Skip to content

Commit baed649

Browse files
committed
Add solution #1911
1 parent 78a5328 commit baed649

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,624 LeetCode solutions in JavaScript
1+
# 1,625 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1469,6 +1469,7 @@
14691469
1906|[Minimum Absolute Difference Queries](./solutions/1906-minimum-absolute-difference-queries.js)|Medium|
14701470
1909|[Remove One Element to Make the Array Strictly Increasing](./solutions/1909-remove-one-element-to-make-the-array-strictly-increasing.js)|Easy|
14711471
1910|[Remove All Occurrences of a Substring](./solutions/1910-remove-all-occurrences-of-a-substring.js)|Medium|
1472+
1911|[Maximum Alternating Subsequence Sum](./solutions/1911-maximum-alternating-subsequence-sum.js)|Medium|
14721473
1920|[Build Array from Permutation](./solutions/1920-build-array-from-permutation.js)|Easy|
14731474
1922|[Count Good Numbers](./solutions/1922-count-good-numbers.js)|Medium|
14741475
1926|[Nearest Exit from Entrance in Maze](./solutions/1926-nearest-exit-from-entrance-in-maze.js)|Medium|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 1911. Maximum Alternating Subsequence Sum
3+
* https://leetcode.com/problems/maximum-alternating-subsequence-sum/
4+
* Difficulty: Medium
5+
*
6+
* The alternating sum of a 0-indexed array is defined as the sum of the elements at even
7+
* indices minus the sum of the elements at odd indices.
8+
* - For example, the alternating sum of [4,2,5,3] is (4 + 5) - (2 + 3) = 4.
9+
*
10+
* Given an array nums, return the maximum alternating sum of any subsequence of nums (after
11+
* reindexing the elements of the subsequence).
12+
*
13+
* A subsequence of an array is a new array generated from the original array by deleting some
14+
* elements (possibly none) without changing the remaining elements' relative order. For
15+
* example, [2,7,4] is a subsequence of [4,2,3,7,2,1,4] (the underlined elements),
16+
* while [2,4,2] is not.
17+
*/
18+
19+
/**
20+
* @param {number[]} nums
21+
* @return {number}
22+
*/
23+
var maxAlternatingSum = function(nums) {
24+
let result = 0;
25+
let oddSum = 0;
26+
27+
for (const num of nums) {
28+
const prevEven = result;
29+
result = Math.max(result, oddSum + num);
30+
oddSum = Math.max(oddSum, prevEven - num);
31+
}
32+
33+
return result;
34+
};

0 commit comments

Comments
 (0)