Algorithm(CodeTree, Python)/완전탐색3

[코드트리 기준을 새로 설정하여 완전탐색] 훌륭한 점프 Python

kurooru 2023. 1. 5. 12:07
# n, k 입력
n, k = map(int, input().split())
# stone_list
stone_list = list(map(int, input().split()))

# 함수들
# is_possible(curr_max)
def is_possible(curr_max):
    
    # idx_list
    idx_list = []
    
    # ston_list를 돌면서
    for i in range(n):
        # 현재 최댓값보다 작거나 같으면
        if stone_list[i] <= curr_max:
            # idx_list에 추가
            idx_list.append(i)
    
    # 처음과 끝 점인이 포함되지 않았다면
    if 0 not in idx_list or n-1 not in idx_list:
        # 실패
        return False

    # idx_list를 돌면서
    for i in range(len(idx_list) - 1):
        # 두 돌 사이의 간격이 한번이라도 k를 넘어서면
        if idx_list[i+1] - idx_list[i] > k:
            # 실패
            return False
    
    # 다 통과시 성공
    return True

# 설계
# max_min
max_min = 0

# 완전 탐색 시작
for i in range(1, 101):
    # 최댓값이 될 수 있는 수라면
    if is_possible(i):
        # max_min update
        max_min = i
        # 반복문 종료
        break

# 출력
print(max_min)