File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ '''
2
+ Given an array of integers arr, return true if and only if it is a
3
+ valid mountain array.
4
+
5
+ Recall that arr is a mountain array if and only if:
6
+ - arr.length >= 3
7
+ - There exists some i with 0 < i < arr.length - 1 such that:
8
+ = arr[0] < arr[1] < ... < arr[i - 1] < A[i]
9
+ = arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
10
+
11
+ Example:
12
+ Input: arr = [2,1]
13
+ Output: false
14
+
15
+ Example:
16
+ Input: arr = [3,5,5]
17
+ Output: false
18
+
19
+ Example:
20
+ Input: arr = [0,3,2,1]
21
+ Output: true
22
+
23
+ Constraints:
24
+ - 1 <= arr.length <= 10^4
25
+ - 0 <= arr[i] <= 10^4
26
+ '''
27
+ #Difficulty: Easy
28
+ #52 / 52 test cases passed.
29
+ #Runtime: 188 ms
30
+ #Memory Usage: 15.6 MB
31
+
32
+ #Runtime: 188 ms, faster than 93.89% of Python3 online submissions for Valid Mountain Array.
33
+ #Memory Usage: 15.6 MB, less than 16.18% of Python3 online submissions for Valid Mountain Array.
34
+
35
+ class Solution :
36
+ def validMountainArray (self , arr : List [int ]) -> bool :
37
+ prev = arr [0 ]
38
+ left = False
39
+ top = False
40
+ for nxt in arr [1 :]:
41
+ if not top and prev < nxt :
42
+ left = True
43
+ elif prev > nxt :
44
+ top = True
45
+ else :
46
+ return False
47
+ prev = nxt
48
+ return left and top
You can’t perform that action at this time.
0 commit comments