-
Notifications
You must be signed in to change notification settings - Fork 43
/
peak-index-in-a-mountain-array.py
155 lines (133 loc) · 3.69 KB
/
peak-index-in-a-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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
852. Peak Index in a Mountain Array
Easy
Let's call an array arr a mountain if the following properties hold:
arr.length >= 3
There exists some i with 0 < i < arr.length - 1 such that:
arr[0] < arr[1] < ... arr[i-1] < arr[i]
arr[i] > arr[i+1] > ... > arr[arr.length - 1]
Given an integer array arr that is guaranteed to be a mountain, return any i such that arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1].
Example 1:
Input: arr = [0,1,0]
Output: 1
Example 2:
Input: arr = [0,2,1,0]
Output: 1
Example 3:
Input: arr = [0,10,5,2]
Output: 1
Constraints:
3 <= arr.length <= 104
0 <= arr[i] <= 106
arr is guaranteed to be a mountain array.
Follow up: Finding the O(n) is straightforward, could you find an O(log(n)) solution?
"""
# V0
# IDEA : PROBLEM UNDERSTANDING
# SAME AS LC 162 Find Peak Element
class Solution(object):
def peakIndexInMountainArray(self, arr):
if len(arr) < 3:
return False
for i in range(len(arr)):
if arr[i] > arr[i+1]:
return i
return -1
# V0'
# IDEA : BINARY SEARCH
class Solution(object):
def peakIndexInMountainArray(self, arr):
if len(arr) < 3:
return False
# binary search
l = 0
r = len(arr) - 1
while r >= l:
mid = l + (r-l)//2
#print ("l = " + str(l) + " r = " + str(r) + " mid = " + str(mid))
if arr[mid] > arr[mid-1] and arr[mid] > arr[mid+1]:
return mid
elif arr[mid] < arr[mid+1]:
l = mid + 1
else:
r = mid - 1
return -1
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/80721162
# IDEA : BINARY SEARCH
class Solution(object):
def peakIndexInMountainArray(self, A):
"""
:type A: List[int]
:rtype: int
"""
left, right = 0, len(A) - 1
while left < right:
mid = (left + right) / 2
if A[mid - 1] < A[mid] and A[mid] < A[mid + 1]:
left = mid
elif A[mid - 1] > A[mid] and A[mid] > A[mid + 1]:
right = mid
else:
break
return mid
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/80721162
# IDEA : BINARY SEARCH
class Solution:
def peakIndexInMountainArray(self, A):
"""
:type A: List[int]
:rtype: int
"""
N = len(A)
left, right = 0, N
while left < right:
mid = left + (right - left) // 2
if A[mid - 1] < A[mid] and A[mid] > A[mid + 1]:
return mid
if A[mid] < A[mid + 1]:
left = mid + 1
else:
right = mid
return -1
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/80721162
# IDEA : MAX
class Solution:
def peakIndexInMountainArray(self, A):
"""
:type A: List[int]
:rtype: int
"""
return A.index(max(A))
# V1'''
# https://blog.csdn.net/fuxuemingzhu/article/details/80721162
# IDEA : FIRST DECREASE
class Solution:
def peakIndexInMountainArray(self, A):
"""
:type A: List[int]
:rtype: int
"""
for i in range(len(A) - 1):
if A[i + 1] < A[i]:
return i
return -1
# V2
# Time: O(logn)
# Space: O(1)
class Solution(object):
def peakIndexInMountainArray(self, A):
"""
:type A: List[int]
:rtype: int
"""
left, right = 0, len(A)
while left < right:
mid = left + (right-left)//2
if A[mid] > A[mid+1]:
right = mid
else:
left = mid+1
return left