코테/Algorithm & 방식

from collections import dequev, e = map(int, input().split())indegree = [0] * (v + 1)graph = [[] for i in range(v + 1)]for _ in range(e): a, b = map(int, input().split()) graph[a].append(b) indegree[b] += 1def topology_sort(): result = [] q = deque() for i in range(1, v + 1): if indegree[i] == 0: q.append(i) while q: now = q.popleft() result.a..
def find_parent(parent, x): if parent[x] != x: return find_parent(parent, parent[x]) return parent[x]def union_parent(parent, a, b): a = find_parent(parent, a) b = find_parent(parent, b) if a
서로소 집합 알고리즘def find_parent(parent, x): if parent[x] != x: return find_parent(parent, parent[x]) return parent[x]def union_parent(parent, a, b): a = find_parent(parent, a) b = find_parent(parent, b) if a     서로소 집합 사이클 판별def find_parent(parent, x): if parent[x] != x: return find_parent(parent, parent[x]) return parent[x]def union_parent(parent, a, b): a = fin..
INF = int(1e9)n = int(input())m = int(input())graph = [[INF] * (n + 1) for _ in range(n + 1)]for a in range(1, n + 1): for b in range(1, n + 1): if a == b: graph[a][b] = 0for _ in range(m): a, b, c = map(int, input().split()) graph[a][b] = cfor k in range(1, n + 1): for a in range(1, n + 1): for b in range(1, n + 1): graph[a][b] = min(graph[a][b], ..
import heapqimport sysinput = sys.stdin.readlineINF = int(1e9)n, m = map(int, input().split())start = int(input())graph = [[] for i in range(n + 1)]distance = [INF] * (n + 1)for _ in range(m): a, b, c = map(int, input().split()) graph[a].append((b, c))def dijkstra(start): q = [] heapq.heappush(q, (0, start)) distance[start] = 0 while q: dist, now = heapq.heappop(q) ..
n = int(input())arr = list(map(int, input().split()))d = [0] * 100# 3 1 1 5# 3 3d[0] = arr[0]d[1] = max(arr[0], arr[1])for i in range(2, n): d[i] = max(d[i - 1], d[i - 2] + arr[i])print(d[n - 1])
재귀함수 사용def binary_search(array, target, start, end): if start > end: return None mid = (start + end) // 2 if array[mid] == target: return mid elif array[mid] > target: return binary_search(array, target, start, mid - 1) else: return binary_search(array, target, mid + 1, end)n, target = list(map(int, input().split()))array = list(map(int, input().split()..
def sequential_search(n, target, array): for i in range(n): if array[i] == target: return i + 1input_data = input().split()n = int(input_data[0])target = input_data[1]array = input().split()print(sequential_search(n, target, array))
array = [7, 5, 9, 0, 3, 1, 6, 2, 9, 1, 4, 8, 0, 5, 2]count = [0] * (max(array) + 1)for i in range(len(array)): count[array[i]] += 1for i in range(len(count)): for j in range(count[i]): print(i, end=' ')
array = [5, 7, 9, 0, 3, 1, 6, 2, 4, 8]def quick_sort(array): if len(array) pivot] return quick_sort(left_side) + [pivot] + quick_sort(right_side)print(quick_sort(array))
array = [7, 5, 9, 0, 3, 1, 6, 2, 4, 8]for i in range(1, len(array)): for j in range(i, 0, -1): if array[j]
array = [7, 5, 9, 0, 3, 1, 6, 2, 4, 8]for i in range(len(array)): min_index = i for j in range(i + 1, len(array)): if array[min_index] > array[j]: min_index = j array[i], array[min_index] = array[min_index], array[i]print(array)
류가든
'코테/Algorithm & 방식' 카테고리의 글 목록