-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeaks.py
More file actions
37 lines (26 loc) · 869 Bytes
/
Copy pathPeaks.py
File metadata and controls
37 lines (26 loc) · 869 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
33
34
35
36
37
def solution(A):
N = len(A)
if N < 3:
return 0
peaks_till_here = get_peaks_till_here(A)
for k in range(2, N + 1):
if N % k == 0 and check_partition(k, peaks_till_here):
return N / k
return 0
def get_peaks_till_here(A):
peaks_till_here = [0] * len(A)
for i in xrange(1, len(A) - 1):
peaks_till_here[i] = peaks_till_here[i - 1]
if A[i - 1] < A[i] > A[i + 1]:
peaks_till_here[i] += 1
peaks_till_here[-1] = peaks_till_here[-2]
return peaks_till_here
def check_partition(k, peaks_till_here):
i = len(peaks_till_here) - 1
while i - k >= 0:
if peaks_till_here[i] <= peaks_till_here[i - k]:
return False
i -= k
if peaks_till_here[k - 1] < 1:
return False
return True