Algorithm144 [백준] 최소비용 구하기 / 1916번 / 파이썬 / python 💡solutions ) 💬 dijkstra(다익스트라) 알고리즘 유형의 기본 문제 🎫code ) import sys import heapq def dijkstra(n, s, e): dist = [INF] * (n + 1) dist[s] = 0 heap = [] heapq.heappush(heap, [0, s]) while heap: cost, node = heapq.heappop(heap) for n, c in adj[node]: c += cost if c < dist[n]: dist[n] = c heapq.heappush(heap, [c, n]) return dist[e] input = sys.stdin.readline n = int(input()) m = int(input()) adj = [[] fo.. 2020. 12. 5. [백준] 숨바꼭질 / 1697번 / 파이썬 / python 🎫code ) from collections import deque # 너비 우선 탐색 n, k = map(int, input().split(' ')) arr = [0] * (k + 1) max_v = 100001 visit = [0] * max_v q = deque([n]) da = [-1, 1, 2] while q: a = q.popleft() if a == k: print(visit[a]) break for i in range(3): if i == 2: na = a * da[i] else: na = a + da[i] # a가 기존, na는 다시 돌아왔을 때 비교 if (0 2020. 12. 4. [백준] 1로 만들기 / 1463번 / 파이썬 / Python 🎫code ) n = int(input()) dp = [0 for _ in range(n + 1)] for i in range(2, n + 1): dp[i] = dp[i - 1] + 1 print(dp, '0') if i % 2 == 0 and dp[i] > dp[i // 2] + 1: dp[i] = dp[i // 2] + 1 print(dp, '1') if i % 3 == 0 and dp[i] > dp[i // 3] + 1: dp[i] = dp[i // 3] + 1 print(dp, '2') print(dp[n]) 📌 description ) 문제출처 : www.acmicpc.net/problem/1463 1463번: 1로 만들기 첫째 줄에 1보다 크거나 같고, 106보다 작거나 같은 정수 N이 주어진.. 2020. 12. 3. [백준] 효율적인 해킹 / 1325번 / 파이썬 / Python 🎫code ) import sys from collections import deque input = sys.stdin.readline def bfs(lst): q = deque(lst) while q: x = q.popleft() visit[x] = 1 for i in adj[x]: if not visit[i]: q.append(i) visit[i] = 1 return visit.count(1) n, m = map(int, input().split(' ')) adj = [[] for _ in range(n + 1)] for _ in range(m): a, b = map(int, input().split(' ')) adj[b].append(a) res = [] max_v = 0 for i in range.. 2020. 12. 1. [백준] 차이를 최대로 / 10819번 / 파이썬 / Python 💡solutions ) 💬 n값이 크지 않으니까, permutations 써서 모든 경우를 찾는 브루트포스 알고리즘 구현 🎫code ) import sys from itertools import permutations as permu input = sys.stdin.readline n = int(input()) arr = list(map(int, input().rstrip().split(' '))) max_v = 0 for p in permu(arr, n): sum = 0 n_list = list(p) for i in range(1, len(n_list)): tmp = abs(n_list[i - 1] - n_list[i]) sum += tmp max_v = max(max_v, sum) print(max_.. 2020. 11. 30. [백준] 그룹 단어 체커 / 1316번 / 파이썬 / Python 🎫code ) cnt = 0 for i in range(int(input())): word = input() word_lst = [word[0]] for k in range(1, len(word)): if word[k] != word[k - 1]: word_lst.append(word[k]) word_set = set(word_lst) if len(word_lst) == len(word_set): cnt += 1 print(cnt) 📌 description ) 문제출처 : www.acmicpc.net/problem/1316 1316번: 그룹 단어 체커 그룹 단어란 단어에 존재하는 모든 문자에 대해서, 각 문자가 연속해서 나타나는 경우만을 말한다. 예를 들면, ccazzzzbb는 c, a, z, b가 모두.. 2020. 11. 29. 이전 1 ··· 8 9 10 11 12 13 14 ··· 24 다음