Skip to content

Commit 673ab3a

Browse files
committed
Add solution #941
1 parent 9248ecd commit 673ab3a

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

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

@@ -750,6 +750,7 @@
750750
938|[Range Sum of BST](./solutions/0938-range-sum-of-bst.js)|Easy|
751751
939|[Minimum Area Rectangle](./solutions/0939-minimum-area-rectangle.js)|Medium|
752752
940|[Distinct Subsequences II](./solutions/0940-distinct-subsequences-ii.js)|Hard|
753+
941|[Valid Mountain Array](./solutions/0941-valid-mountain-array.js)|Easy|
753754
966|[Vowel Spellchecker](./solutions/0966-vowel-spellchecker.js)|Medium|
754755
970|[Powerful Integers](./solutions/0970-powerful-integers.js)|Easy|
755756
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 941. Valid Mountain Array
3+
* https://leetcode.com/problems/valid-mountain-array/
4+
* Difficulty: Easy
5+
*
6+
* Given an array of integers arr, return true if and only if it is a valid mountain array.
7+
*
8+
* Recall that arr is a mountain array if and only if:
9+
* - arr.length >= 3
10+
* - There exists some i with 0 < i < arr.length - 1 such that:
11+
* - arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
12+
* - arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
13+
*/
14+
15+
/**
16+
* @param {number[]} arr
17+
* @return {boolean}
18+
*/
19+
var validMountainArray = function(arr) {
20+
if (arr.length < 3) return false;
21+
22+
const peakIndex = arr.indexOf(Math.max(...arr));
23+
if (peakIndex === 0 || peakIndex === arr.length - 1) return false;
24+
25+
for (let i = 1; i < peakIndex; i++) {
26+
if (arr[i] <= arr[i - 1]) return false;
27+
}
28+
29+
for (let i = peakIndex + 1; i < arr.length; i++) {
30+
if (arr[i] >= arr[i - 1]) return false;
31+
}
32+
33+
return true;
34+
};

0 commit comments

Comments
 (0)