본문 바로가기

알고리즘/백준 (Pyhthon)

(79)
[알고리즘] 백준 1991 트리순회 / python class Node: def __init__(self, data, left_node, right_node): self.data = data self.left_node = left_node self.right_node = right_node def pre_order(node): print(node.data, end='') if node.left_node != '.': pre_order(tree[node.left_node]) if node.right_node != '.': pre_order(tree[node.right_node]) def in_order(node): if node.left_node != '.': in_order(tree[node.left_node]) print(node.data, end=''..
[알고리즘] 백준 1939 중량제한 / python from collections import deque n, m = map(int, input().split(' ')) def bfs(c): queue = deque() queue.append(start) visited = [False] * (n+1) visited[start] = True while queue: x = queue.popleft() for y, weight in adj[x]: if not visited[y] and weight >= c: queue.append(y) visited[y] = True return visited[end] adj = [ [] for _ in range(n+1) ] max_weight = 1 min_weight = 1000000000 for _ in range(m)..
[알고리즘] 백준 1236 성지키기 n, m = map(int, input().split(' ')) arr = [] for _ in range(n): arr.append(input()) row = [0] * n col = [0] * m for i in range(n): for j in range(m): if arr[i][j] == 'X': row[i] = 1 col[j] = 1 row_count, col_count = 0, 0 for i in row: if i == 0: row_count += 1 for i in col: if i == 0: col_count += 1 print(max(row_count,col_count)) 배열에 문자열을 n번 append하면 이차원배열 형식으로 접근가능하다는,, arr.append('abcd') arr...
[알고리즘] 백준 1668 트로피진열 def display(arr): count = 1 now = arr[0] for i in range(1,len(arr)): if now 4 2
[알고리즘] 백준 1302 베스트셀러 n = int(input()) book_list = {} for _ in range(n): book = input() if book in book_list: book_list[book] += 1 else: book_list[book] = 1 target = max(book_list.values()) array = [] for k, v in book_list.items(): if v == target: array.append(k) book_max = [] book_max = sorted(array) print(book_max[0])
[알고리즘] 백준 1568 새 n = int(input()) k = 1 count = 0 while n > 0: if n < k: k = 1 else: n -= k k += 1 count += 1 print(count)
[알고리즘] 백준 1543 문서검색 / python doc = input() search = input() count = 0 i = 0 while i
[알고리즘] 백준 2110 공유기 설치 n, c = map(int, input().split(" ")) location = [] for _ in range(n): location.append(int(input())) location.sort() start = location[1] - location[0] end = location[-1] - location[0] ans = 1 while start