File tree 2 files changed +36
-1
lines changed
2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change 1
- # LeetCode solutions in JavaScript
1
+ # 1,026 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcode.com/ ] ( https://leetcode.com/ )
4
4
750
750
938|[ Range Sum of BST] ( ./solutions/0938-range-sum-of-bst.js ) |Easy|
751
751
939|[ Minimum Area Rectangle] ( ./solutions/0939-minimum-area-rectangle.js ) |Medium|
752
752
940|[ Distinct Subsequences II] ( ./solutions/0940-distinct-subsequences-ii.js ) |Hard|
753
+ 941|[ Valid Mountain Array] ( ./solutions/0941-valid-mountain-array.js ) |Easy|
753
754
966|[ Vowel Spellchecker] ( ./solutions/0966-vowel-spellchecker.js ) |Medium|
754
755
970|[ Powerful Integers] ( ./solutions/0970-powerful-integers.js ) |Easy|
755
756
976|[ Largest Perimeter Triangle] ( ./solutions/0976-largest-perimeter-triangle.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments