-
Notifications
You must be signed in to change notification settings - Fork 43
/
valid-mountain-array.py
84 lines (80 loc) · 2.14 KB
/
valid-mountain-array.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# V0
# V1
# https://blog.csdn.net/GQxxxxxl/article/details/84247251
# IDEA : TWO POINTER
# -> SET UP TWO POINTERS : LEFT, RIGHT (AT LEFT END AND RIGHT END)
# -> IF A[LEFT] < A[LEFT +1] : MOVE LEFT TO RIGHT (LEFT = LEFT + 1 )
# -> IF A[RIGHT] < A[RIGHT - 1] : MOVE RIGHT TO LEFT (RIGHT = RIGHT - 1 )
# -> CHECK WHETHER TWO POINTER LEFT, RIGHT OVERLAP
class Solution:
def validMountainArray(self, A):
"""
:type A: List[int]
:rtype: bool
"""
if len(A)<3:
return False
left,right=0,len(A)-1
while left<len(A)-1 and A[left]<A[left+1]:
left+=1
while right>0 and A[right]<A[right-1]:
right-=1
return left!=0 and right!=len(A)-1 and left==right
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/84206380
class Solution:
def validMountainArray(self, A):
"""
:type A: List[int]
:rtype: bool
"""
N = len(A)
if N < 3:
return False
i = 0
while i < N - 1:
if A[i] < A[i + 1]:
i += 1
else:
break
if i == 0 or i == N - 1: return False
while i < N - 1:
if A[i] > A[i + 1]:
i += 1
else:
break
return i == N - 1
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/84206380
class Solution(object):
def validMountainArray(self, A):
"""
:type A: List[int]
:rtype: bool
"""
print(A)
N = len(A)
if N < 3: return False
i = 0
while i < N - 1 and A[i + 1] > A[i]:
i += 1
if i == 0 or i == N - 1: return False
while i < N - 1 and A[i] > A[i + 1]:
i += 1
return i == N - 1
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
def validMountainArray(self, A):
"""
:type A: List[int]
:rtype: bool
"""
i = 0
while i+1 < len(A) and A[i] < A[i+1]:
i += 1
j = len(A)-1
while j-1 >= 0 and A[j-1] > A[j]:
j -= 1
return 0 < i == j < len(A)-1