본문 바로가기

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

[코드트리 기준을 새로 설정하여 완전탐색] 최대 최소간의 차 Python # n, k 입력 n, k = map(int, input().split()) # num_list 입력 num_list = list(map(int, input().split())) # 함수들 # get_cost_from_bottom(num_to_add) def get_cost_from_bottom(num_to_add): # curr_min_num curr_min_num = min_num + num_to_add # needed_cost needed_cost = 0 # num_list를 돌면서 for num in num_list: # curr_min_num보다 작은 값을 발견하면 if num < curr_min_num: # 필요한 비용에 추가 needed_cost += curr_min_num - num # 반.. 2023. 1. 10.
[코드트리 기준을 새로 설정하여 완전탐색] 최대 H 점수 2 Python # n, l 입력 n, l = map(int, input().split()) # num_list num_list = list(map(int, input().split())) # 함수들 # is_possible(curr_h_point) def is_possible(curr_h_point): # pass_cnt pass_cnt = 0 # chance_point chance_point = l # num_list를 돌며 for i in range(n): # curr_h_point 이상 값은 if num_list[i] >= curr_h_point: # pass_cnt에 추가 pass_cnt += 1 # curr_h_point와 1 차이나는데, chance_point가 남아있다면 elif num_list[i] + .. 2023. 1. 9.
[코드트리 기준을 새로 설정하여 완전탐색] 구간 잘 나누기 Python 처음으로 답지보고 풀었다. 최소의 최대, 최소의 최대 이런 문제 풀 때는 반드시 그 범위를 설정하고 하나씩 가능한 값인지 생각하는 아이디어를 기억해야겠다. # n, m 입력 n, m = map(int, input().split()) # num_list num_list = list(map(int, input().split())) # 함수들 # is_possible(curr_possible_max) def is_possible(curr_possible_max): # curr_sum curr_sum = 0 # curr_section curr_section = 1 for i in range(n): # 하나라도 숫자가 curr_possible_max보다 크면 if num_list[i] > curr_possible.. 2023. 1. 8.
[코드트리 기준을 새로 설정하여 완전탐색] 초기 수열 복원하기 Python 상황에 따라 수열의 길이가 바뀌는 것을 어떻게 처리해줘야할지 고민하게 되었다. recursion을 사용하지 않고 최대한 완전탐색답게 풀어보려 하였으나 quit() 함수를 사용하여 문제를 해결하였을 때의 이 찜찜함이란,, # n 입력 n = int(input()) # sum_list sum_list = list(map(int, input().split())) # 함수들 # is_duplicated(curr_num_list) def is_duplicated(curr_num_list): # 전수 검사 for i in range(n): # curr_num => 현재 기준이 되는 수 curr_num = curr_num_list[i] for j in range(n): # 자기 자신이면 if i == j: # pas.. 2023. 1. 7.
[코드트리 기준을 새로 설정하여 완전탐색] 언덕 깎기 Python # n 입력 n = int(input()) # hills 입력 hills = [ int(input()) for _ in range(n) ] # 함수들 # get_cost(to_add, to_minus) def get_cost(to_add, to_minus): # new_lowest_hill, new_highest_hill new_lowest_hill, new_highest_hill = lowest_hill + to_add, highest_hill - to_minus # curr_cost curr_cost = 0 # 모든 new_lowest_hill미만 높이의 hill을 # new_lowest_hill로 만드는 비용 계산 for hill in hills: if hill < new_lowest_hill: c.. 2023. 1. 6.
[코드트리 기준을 새로 설정하여 완전탐색] 독서실의 거리두기 4 Python # n 입력 n = int(input()) # study_cafe 입력 study_cafe = input() # 함수들 # get_min_dist(idx_1, idx_2) def get_min_dist(idx_1, idx_2): # seats_filled_with_user seats_filled_with_user = [idx_1, idx_2] # 독서실을 돌며 for i in range(n): # 자리에 사람이 있으면 if study_cafe[i] == '1': # 위치를 기록 seats_filled_with_user.append(i) # seats_filled_with_user 정렬 seats_filled_with_user.sort() # num_of_filled_seats num_of_filled_s.. 2023. 1. 5.
[코드트리 기준을 새로 설정하여 완전탐색] 훌륭한 점프 Python # 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] k: # 실패 return False # 다 통과시 성공 return True # 설계 # max_min max_min = 0 # 완전 탐색 시작 for i in range(1, 101): # 최댓값이 될 수 있는 수라면 if is_possible(i): .. 2023. 1. 5.
[코드트리 기준을 새로 설정하여 완전탐색] 숫자들의 최대 차 Python # n, k 입력 n, k = map(int, input().split()) # num_list num_list = list() # num_list 입력 for _ in range(n): num_list.append(int(input())) # 함수들 # is_possible(pick_num) def is_possible(pick_num): # 정렬된 num_list를 돌면서 for i in range(n - pick_num + 1): # curr_min, curr_max curr_min, curr_max = num_list[i], num_list[i + pick_num -1] # 한번이라도 차가 k이하면 if curr_max - curr_min 뽑을 숫자의 개수를 기준으로 for i in range(2.. 2023. 1. 4.
[코드트리 기준을 새로 설정하여 완전탐색] 이상한 폭탄 3 Python # 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.. 2023. 1. 3.
[코드트리 기준을 새로 설정하여 완전탐색] A, B, C, D 찾기 2 Python # num_list 입력 num_list = list(map(int, input().split())) # 함수들 # is_possible(a, b, c, d) def is_possible(a, b, c, d): # temp_list temp_list = [a, b, c, d, a+b, b+c, c+d, d+a, a+c, b+d, a+b+c, a+b+d, a+c+d, b+c+d, a+b+c+d] # temp_list 정렬 temp_list.sort() # 반환 return temp_list == num_list # 설계 # num_list 정렬 num_list.sort() # 완전 탐색 시작 for i in range(1, 41): for j in range(i, 41): for k in range(j, .. 2023. 1. 3.
[코드트리 기준을 새로 설정하여 완전탐색] 가장 많이 나온 쌍 Python # n, m 입력 n, m = map(int, input().split()) # num_set num_set = [] # a, b 입력 for _ in range(m): num_set.append(tuple(map(int, input().split()))) # 함수들 # calc(a, b) def calc(a, b): # curr_cnt curr_cnt = 0 for num in num_set: # unpacking num_1, num_2 = num # a, b 와 같은 조합이면 if (num_1 == a and num_2 == b) or (num_1 == b and num_2 == a): # curr_cnt 올려주기 curr_cnt += 1 # 반환 return curr_cnt # 설계 # max_cn.. 2023. 1. 2.
[코드트리 기준을 새로 설정하여 완전탐색] 가장 작은 x 찾기 Python # n 입력 n = int(input()) # range_list range_list = list() # range_list 입력 for _ in range(n): range_list.append(tuple(map(int, input().split()))) # 함수들 # success(k) def success(k): # curr_num curr_num = k for i in range(n): # unpacking a, b = range_list[i] # 한번이라도 범위에 해당하지 않으면 if not (a 2023. 1. 1.
[코드트리 기준을 새로 설정하여 완전탐색] 독서실의 거리두기 5 Python # n 입력 n = int(input()) # study_cafe 입력 study_cafe = input() # 함수들 # get_max_dist(curr_study_cafe) def get_max_dist(curr_study_cafe): # people_pos people_pos = [] # curr_study_cafe 돌면서 for i in range(n): # 사람이 있으면 if curr_study_cafe[i]: # people_pos에 기록 people_pos.append(i) # people_num -> 현재 독서실에 있는 사람의 수 people_num = len(people_pos) # curr_min_dist curr_min_dist = sys.maxsize for i in range(pe.. 2022. 12. 31.
[코드트리 상황을 일일이 가정해보고 진행하는 완전탐색] 원소 값들의 최대 합 Python # n, m 입력 n, m = map(int, input().split()) # num_list 입력 num_list = list(map(int, input().split())) # 함수들 # calc(start_idx) def calc(start_idx): # temp_list temp_list = num_list[:] # curr_idx, curr_sum curr_idx, curr_sum = start_idx, 0 # m 번의 움직임 for _ in range(m): # 이동할 숫자 curr_sum에 담아줌 curr_sum += temp_list[curr_idx] # curr_idx 바꿔줌 curr_idx = temp_list[curr_idx] - 1 # 반환 return curr_sum # 설계 #.. 2022. 12. 30.
[코드트리 상황을 일일이 가정해보고 진행하는 완전탐색] 등장하지 않는 문자열의 길이 Python # n 입력 n = int(input()) # string 입력 string = input() # 함수들 # exists_more_than_two_times(search_str) def exists_more_than_two_times(search_str): # 검색하는 문자열 중 for s in search_str: # curr_cnt curr_cnt = 0 # 같은 문자열 내에서 for another_s in search_str: # 자신과 같은 문자열을 발견하면 if s == another_s: # curr_cnt 올려주기 curr_cnt += 1 # 한번이라도 두 번 이상 있었으면 if curr_cnt >= 2: # 성공 return True # 없었으면 실패 return False # get_cu.. 2022. 12. 29.
[코드트리 상황을 일일이 가정해보고 진행하는 완전탐색] 좌표평면 위의 균형 2 Python # n 입력 n = int(input()) # point_pos point_pos = list() # x, y 입력 for _ in range(n): point_pos.append(tuple(map(int, input().split()))) # 함수들 # get_q1(x, y) def get_q1(x, y): # point_cnt point_cnt = 0 # point_pos를 돌면서 for point in point_pos: # unpacking p_x, p_y = point # 1사분면에 위치해 있으면 if p_x > x and p_y > y: # point_cnt 올려주기 point_cnt += 1 # 반환 return point_cnt # get_q2(x, y) def get_q2(x, y): #.. 2022. 12. 29.
[코드트리 상황을 일일이 가정해보고 진행하는 완전탐색] 팀으로 하는 틱택토 2 Python # table 입력 table = [ list(input()) for _ in range(3) ] # search_row(a, b) def search_row(a, b): # 모든 행을 조사 for i in range(3): # 한번이라도 성공하면 if (table[i][0] == a or table[i][0] == b) and \ (table[i][1] == a or table[i][1] == b) and \ (table[i][2] == a or table[i][2] == b) and \ not (table[i][0] == table[i][1] and table[i][1] == table[i][2]): # 성공 return True # 이외에는 실패 return False # search_col(a, .. 2022. 12. 28.
[코드트리 상황을 일일이 가정해보고 진행하는 완전탐색] 수를 여러번 사용하여 특정 수 만들기 Python a, b, c = map(int, input().split()) def get_max_num(m, M): curr_max = 0 while True: if m * curr_max > M: return curr_max else: curr_max += 1 def calc(first, second): curr_sum = a * first + b * second if curr_sum > c: return 0 else: return curr_sum max_len = get_max_num(a, c) max_num = 0 for i in range(max_len): for j in range(max_len): max_num = max(max_num, calc(i,j)) print(max_num) 2022. 12. 27.