Skip to content

Commit b50563d

Browse files
committed
Add solution #2016
1 parent 5656c93 commit b50563d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
1880|[Check if Word Equals Summation of Two Words](./1880-check-if-word-equals-summation-of-two-words.js)|Easy|
117117
1929|[Concatenation of Array](./1929-concatenation-of-array.js)|Easy|
118118
2000|[Reverse Prefix of Word](./2000-reverse-prefix-of-word.js)|Easy|
119+
2016|[Maximum Difference Between Increasing Elements](./2016-maximum-difference-between-increasing-elements.js)|Easy|
119120

120121
## License
121122

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 2016. Maximum Difference Between Increasing Elements
3+
* https://leetcode.com/problems/maximum-difference-between-increasing-elements/
4+
* Difficulty: Easy
5+
*
6+
* Given a 0-indexed integer array nums of size n, find the maximum difference
7+
* between nums[i] and nums[j] (i.e., nums[j] - nums[i]), such that
8+
* 0 <= i < j < n and nums[i] < nums[j].
9+
*
10+
* Return the maximum difference. If no such i and j exists, return -1.
11+
*/
12+
13+
/**
14+
* @param {number[]} nums
15+
* @return {number}
16+
*/
17+
var maximumDifference = function(nums) {
18+
let max = 0;
19+
20+
for (let i = 0, min = nums[0]; i < nums.length; i++) {
21+
min = Math.min(min, nums[i]);
22+
max = Math.max(max, nums[i] - min);
23+
}
24+
25+
return max === 0 ? -1 : max;
26+
};

0 commit comments

Comments
 (0)