Skip to content

Commit d16be5a

Browse files
Create 941.ValidMountainArray.py
1 parent 7262415 commit d16be5a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Easy/941.ValidMountainArray.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

0 commit comments

Comments
 (0)