codility에서는 공식적으로 두가지 방법에 대해 소개하고 있다
def solution(A):
length = len(A)
peak_array = [0 for _ in range(length)]
peak_length = 0
for i, _ in enumerate(A):
if i - 1 < 0:
continue
if i + 1 >= length:
continue
if A[i-1] < A[i] and A[i+1] < A[i]:
peak_array[i] = 1
peak_length += 1
last_num = 0
for flags in range(1, peak_length + 1):
pos = 0
flag_num = flags
while flags > 0 and pos < length:
if peak_array[pos]:
flags -= 1
pos += flag_num
else:
pos += 1
if flags == 0 :
last_num = flag_num
else:
break
return last_num
def solution(A):
length = len(A)
peak_array = [0 for _ in range(length)]
peak_length = 0
for i, _ in enumerate(A):
if i - 1 < 0:
continue
if i + 1 >= length:
continue
if A[i-1] < A[i] and A[i+1] < A[i]:
peak_array[i] = 1
peak_length += 1
#print(i)
next_peak = [0 for _ in range(length)]
next_peak[-1] = -1
for i in range(length - 2, -1, -1):
if peak_array[i] == 1:
next_peak[i] = i
else:
next_peak[i] = next_peak[i+1]
flag = 1
result = 0
#print(next_peak)
while (flag - 1) * flag <= length:
pos = 0
num = 0
while pos < length and num < flag:
pos = next_peak[pos]
#print("pos :: " + str(pos))
if pos == -1:
break
num += 1
pos += flag
result = max(result, num)
flag += 1
return result
def solution(A):
length = len(A)
peak_array = [0 for _ in range(length)]
peak_length = 0
for i, _ in enumerate(A):
if i - 1 < 0:
continue
if i + 1 >= length:
continue
if A[i-1] < A[i] and A[i+1] < A[i]:
peak_array[i] = 1
peak_length += 1
#print(i)
next_peak = [0 for _ in range(length)]
next_peak[-1] = -1
for i in range(length - 2, -1, -1):
if peak_array[i] == 1:
next_peak[i] = i
else:
next_peak[i] = next_peak[i+1]
flag = 1
result = 0
#print(next_peak)
while flag <= length:
pos = 0
num = 0
while pos < length and num < flag:
pos = next_peak[pos]
#print("pos :: " + str(pos))
if pos == -1:
break
num += 1
pos += flag
result = max(result, num)
flag += 1
return result