Algorithm(CodeTree, Python)/완전탐색3
[코드트리 기준을 새로 설정하여 완전탐색] 이상한 폭탄 3 Python
kurooru
2023. 1. 3. 15:02
# n, k 입력
n, k = map(int, input().split())
# bomb_list
bomb_list = list()
# bomb 입력
for _ in range(n):
bomb_list.append(int(input()))
# 함수들
# is_about_to_blow_up(curr_idx)
def is_about_to_blow_up(curr_idx):
# curr_bomb_num
curr_bomb_num = bomb_list[curr_idx]
# curr_idx이후부터 k번 후까지 탐색
for i in range(curr_idx+1, curr_idx + k + 1):
# 같은 번호의 폭탄이 있으면,
if bomb_list[i] == curr_bomb_num:
# 터짐
return True
# 다 확인했는데 없으면 안터짐
return False
# get_bomb_nums_and_num(curr_idx)
def get_bomb_nums_and_num(curr_idx):
# curr_bomb_num
curr_bomb_num = bomb_list[curr_idx]
# curr_bomb_nums
curr_bomb_nums = 0
# curr_idx부터 k번 후까지 탐색
for i in range(curr_idx, curr_idx + k + 1):
# 같은 번호의 폭탄이 있으면,
if bomb_list[i] == curr_bomb_num:
# curr_bomb_nums 올려주기
curr_bomb_nums += 1
# 터질 폭탄의 개수와
# 폭탄의 번호를 tuple 형태로 반환
return (curr_bomb_nums, curr_bomb_num)
# 설계
# bombs_about_to_blow_up
bombs_about_to_blow_up = []
# 완전 탐색 시작
for i in range(n - k):
# 폭팔할 폭탄이면
if is_about_to_blow_up(i):
# 개수와 폭탄 번호를 기록
bombs_about_to_blow_up.append(get_bomb_nums_and_num(i))
# bombs_about_to_blow_up 정렬
bombs_about_to_blow_up.sort()
# 터질 폭탄이 없다면
if not bombs_about_to_blow_up:
# 0 출력
print(0)
# 이외에는
else:
# unpacking
bomb_nums, bomb_num = bombs_about_to_blow_up[-1]
# 출력
print(bomb_num)