Skip to content

Commit 48d7a03

Browse files
committed
Add solution #2874
1 parent 83ab379 commit 48d7a03

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-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,125 LeetCode solutions in JavaScript
1+
# 1,126 LeetCode solutions in JavaScript
22

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

@@ -1105,6 +1105,7 @@
11051105
2780|[Minimum Index of a Valid Split](./solutions/2780-minimum-index-of-a-valid-split.js)|Medium|
11061106
2818|[Apply Operations to Maximize Score](./solutions/2818-apply-operations-to-maximize-score.js)|Hard|
11071107
2873|[Maximum Value of an Ordered Triplet I](./solutions/2873-maximum-value-of-an-ordered-triplet-i.js)|Easy|
1108+
2874|[Maximum Value of an Ordered Triplet II](./solutions/2874-maximum-value-of-an-ordered-triplet-ii.js)|Medium|
11081109
2948|[Make Lexicographically Smallest Array by Swapping Elements](./solutions/2948-make-lexicographically-smallest-array-by-swapping-elements.js)|Medium|
11091110
2965|[Find Missing and Repeated Values](./solutions/2965-find-missing-and-repeated-values.js)|Easy|
11101111
3042|[Count Prefix and Suffix Pairs I](./solutions/3042-count-prefix-and-suffix-pairs-i.js)|Easy|
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 2874. Maximum Value of an Ordered Triplet II
3+
* https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed integer array nums.
7+
*
8+
* Return the maximum value over all triplets of indices (i, j, k) such that i < j < k. If all
9+
* such triplets have a negative value, return 0.
10+
*
11+
* The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].
12+
*/
13+
14+
/**
15+
* @param {number[]} nums
16+
* @return {number}
17+
*/
18+
var maximumTripletValue = function(nums) {
19+
let result = 0;
20+
let maxNum = nums[0];
21+
let maxDiff = 0;
22+
23+
for (let i = 1; i < nums.length; i++) {
24+
result = Math.max(result, maxDiff * nums[i]);
25+
maxDiff = Math.max(maxDiff, maxNum - nums[i]);
26+
maxNum = Math.max(maxNum, nums[i]);
27+
}
28+
29+
return result;
30+
};

0 commit comments

Comments
 (0)