Programming61 [이취코] 금광 (Python3) DP를 적용한 코드 for tc in range(int(input())): n, m = map(int, input().split()) array = list(map(int, input().split())) dp = [] index = 0 for i in range(n): dp.append(array[index:index + m]) index += m # print(dp) for j in range(1, m): for i in range(n): if i == 0: left_up = 0 else: left_up = dp[i-1][j-1] if i == n-1: left_down = 0 else: left_down = dp[i+1][j-1] left = dp[i][j-1] dp[i][j] = dp[i][j] .. 2024. 3. 6. [이취코] 효율적인 화폐 구성 (Python3) 처음에 짠 코드 n, m = map(int, input().split()) costs = [int(input()) for _ in range(n)] count = -1 d = [0] * 10000 for cost in costs: if m % cost == 0: count = m // cost else: count = -1 print(count) DP를 적용한 코드 n, m = map(int, input().split()) cost = [int(input()) for _ in range(n)] d = [10001] * (m+1) d[0] = 0 for i in range(n): # 3 for j in range(cost[i], m+1): # cost ~ 7 if d[j - cost[i]] != 10001.. 2024. 3. 6. [이취코] 1로 만들기 (Python3) 처음에 짠 코드 n = int(input()) count = 0 if n == 1: count = 0 if n == 2: count = 1 if n == 3: count = 1 if n == 5: count = 1 else: while n != 1: if n % 5 == 0: n /= 5 count += 1 elif n % 3 == 0: n /= 3 count += 1 elif n % 2 == 0: n /= 2 count += 1 else: n -= 1 count += 1 print(count) DP 를 적용한 코드 n = int(input()) d = [0] * 30001 for i in range(2, n + 1): d[i] = d[i - 1] + 1 if i % 2 == 0: d[i] = min(d.. 2024. 3. 6. [이취코] 정렬된 수열에서 특정 수의 개수 구하기 (Python) 처음에 짠 코드 n, m = list(map(int, input().split())) arr = list(map(int, input().split())) start = arr[0] end = arr[-1] count = 0 while start m: end = mid - 1 else: start = mid + 1 print(count) BS를 적용한 코드 from bisect import bisect_left, bisect_right def count_by_range(array, left_value, right_value): right_index = bisect_right(array, right_value) left_index = bisect_left(array, left_value) return rig.. 2024. 3. 6. [이취코] 떡볶이 떡 만들기 (Python) 처음에 짠 코드 import sys n, m = map(int, sys.stdin.readline().split()) arr = list(map(int, sys.stdin.readline().split())) sorted_arr = sorted(arr) end = sorted_arr[n-1] # 19 start = sorted_arr[0] for i in range(end - 1, start, -1): count = 0 for j in range(len(sorted_arr)): if sorted_arr[j] - i >= 0: count += sorted_arr[j] - i if count == m: break print(i) BS를 적용한 코드 n, m = list(map(int, input().spli.. 2024. 3. 6. [BaekJoon] 바이러스 (Python3) https://www.acmicpc.net/problem/2606 2606번: 바이러스 첫째 줄에는 컴퓨터의 수가 주어진다. 컴퓨터의 수는 100 이하인 양의 정수이고 각 컴퓨터에는 1번 부터 차례대로 번호가 매겨진다. 둘째 줄에는 네트워크 상에서 직접 연결되어 있는 컴퓨터 쌍 www.acmicpc.net DFS와 BFS는 아직 좀 어렵다... 많은 연습을 필요로 할 듯 하다~ DFS n = int(input()) v = int(input()) graph = [[] for i in range(n+1)] visited = [0] * (n+1) for i in range(v): a, b = map(int, input().split()) graph[a] += [b] graph[b] += [a] def dfs(.. 2024. 2. 17. 이전 1 2 3 4 5 6 ··· 11 다음