-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path941.py
38 lines (32 loc) · 1.07 KB
/
941.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
__________________________________________________________________________________________________
sample 216 ms submission
class Solution:
def validMountainArray(self, A: List[int]) -> bool:
if len(A)<3:
return False
l=len(A)
i,j=0,l-1
while i<j and A[i]<A[i+1]:
i+=1
while j>0 and A[j]<A[j-1]:
j-=1
if i==j and j!=l-1 and i!=0:
return True
return False
__________________________________________________________________________________________________
sample 13848 kb submission
class Solution:
def validMountainArray(self, A: List[int]) -> bool:
N = len(A)
i = 0
# walk up
while i+1 < N and A[i] < A[i+1]:
i += 1
# peak can't be first or last
if i == 0 or i == N-1:
return False
# walk down
while i+1 < N and A[i] > A[i+1]:
i += 1
return i == N-1
__________________________________________________________________________________________________