-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeak element.py
More file actions
32 lines (29 loc) · 756 Bytes
/
Copy pathPeak element.py
File metadata and controls
32 lines (29 loc) · 756 Bytes
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
class Solution:
def peakElement(self, arr, n):
arr.insert(0, 0)
arr.insert(n + 1, 0)
for i in range(1, len(arr) - 1):
if arr[i] >= arr[i - 1] and arr[i] >= arr[i + 1]:
return i - 1
return -1
n = 3
arr = [1, 2, 3]
index = Solution().peakElement(arr.copy(), n)
flag = False
if index < 0 or index >= n:
flag = False
else:
if index == 0 and n == 1:
flag = True
elif index == 0 and arr[index] >= arr[index + 1]:
flag = True
elif index == n - 1 and arr[index] >= arr[index - 1]:
flag = True
elif arr[index - 1] <= arr[index] and arr[index] >= arr[index + 1]:
flag = True
else:
flag = False
if flag:
print(1)
else:
print(0)