본문 바로가기

Algorithm(CodeTree, Python)127

[코드트리] 컨베이어 벨트 Python # n, t 입력 n, t = map(int, input().split()) # conbeyor_belt 입력 conbeyor_belt = [ list(map(int, input().split())) for _ in range(2) ] # 함수들 # simulate() def simulate(): # temp_up, temp_down temp_up, temp_down = conbeyor_belt[0][-1], conbeyor_belt[1][-1] # 옮겨주기 for i in range(n-1, 0, -1): # 윗 줄 conbeyor_belt[0][i] = conbeyor_belt[0][i-1] # 아랫 줄 conbeyor_belt[1][i] = conbeyor_belt[1][i-1] # temp_up,.. 2023. 1. 15.
[코드트리] 양수 직사각형의 최대 크기 Python 직사각형 형성의 조건을 생각해보면 간단하게 해결할 수 있다. 대각선을 기준으로 시작점과 끝점을 잡으면 직사각형의 범위를 잡을 수 있다. # n, m 입력 n, m = map(int, input().split()) # grid 입력 grid = [ list(map(int, input().split())) for _ in range(n) ] # 함수들 # is_plus(sx, sy, ex, ey) def is_plus(sx, sy, ex, ey): # (sx, sy) 에서 (ex, ey)까지 탐색 for i in range(sx, ex + 1): for j in range(sy, ey + 1): # 한번이라도 양수가 아니면 if grid[i][j] 끝점 for i in range(x, n): for j in.. 2023. 1. 14.
[코드트리] 금 채굴하기 Python 마름모 구하는데 4시간 걸렸다,, 자만하지말자,, 마름모가 정사각형을 유지하며 커지지 않는다는 것을 왜 3시간동안 몰랐을까,, # n, m 입력 n, m = map(int, input().split()) # grid 입력 grid = [ list(map(int, input().split())) for _ in range(n) ] # 함수들 # get_cost(s) def get_cost(s): return s * s + (s+1) * (s+1) # in_range(x, y) def in_range(x, y): return 0 마름모의 크기 for k in range(2 * n): # income, cost income, cost = get_income(i, j, k), get_cost(k) # incom.. 2023. 1. 13.
[코드트리] 트로미노 Python # n, m 입력 n, m = map(int, input().split()) # grid 입력 grid = [ list(map(int, input().split())) for _ in range(n) ] # 함수들 # in_range_case_1(x, y) def in_range_case_1(x, y): # x는 n-1보다 작고, y는 m-1보다 작으면 통과 return x < n-1 and y < m-1 # get_case_1(x, y) def get_case_1(x, y): # 90도씩 돌린 4 가지 케이스를 전수조사 # sum_1 sum_1 = grid[x][y] + grid[x+1][y] + grid[x+1][y+1] # sum_2 sum_2 = grid[x+1][y] + grid[x+1][y+1.. 2023. 1. 12.
[코드트리] 행복한 수열의 개수 Python # n, m 입력 n, m = map(int, input().split()) # grid grid = [ list(map(int, input().split())) for _ in range(n) ] # 함수들 # is_happy_row(idx) def is_happy_row(idx): # curr_cnt curr_cnt = 1 for i in range(n - 1): # 다음 숫자와 같으면 if grid[idx][i] == grid[idx][i+1]: # curr_cnt 올려주기 curr_cnt += 1 # curr_cnt가 m이 되었으면 if curr_cnt == m: # 성공 return True # 다음 숫자와 다르면 elif grid[idx][i] != grid[idx][i+1]: # curr_c.. 2023. 1. 11.
[코드트리] 최고의 33위치 Python # n 입력 n = int(input()) # grid grid = [ list(map(int, input().split())) for _ in range(n) ] # 함수들 # in_range(x, y) def in_range(x, y): # 격자를 벗어나지 않는 지 반환 return x + 2 < n and y + 2 < n # get_coin(x, y) def get_coin(x, y): # curr_coin curr_coin= 0 # 현 범위 돌면서 for i in range(x, x + 3): for j in range(y, y + 3): # curr_coin 구하기 curr_coin += grid[i][j] # 반환 return curr_coin # 설계 # max_coin max_coin =.. 2023. 1. 11.
[코드트리 기준을 새로 설정하여 완전탐색] 최대 최소간의 차 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.