본문 바로가기

알고리즘124

연구소[백준14502] - python 정답 코드 import sys import copy from collections import deque from itertools import combinations input = sys.stdin.readline directions = [[1,0], [-1,0], [0,1], [0,-1]] def bfsAndCountZero(array, viruses) : height = len(array) width = len(array[0]) visited = [[0] * width for _ in range(height)] zero_count = 0 queue = deque() for virus in viruses : queue.append(virus) while queue : curr_x, curr_y = q.. 2021. 12. 26.
LCS2 [백준 9252] - python def BOJ9252(): first = input() second = input() array = [[[0, '']] * (len(second) + 1) for _ in range(len(first) + 1)] l = [] for i in range(1, len(first) + 1): for j in range(1, len(second) + 1): if first[i-1] == second[j-1]: array[i][j] = [array[i-1][j-1][0] + 1, array[i-1][j-1][1] + first[i-1]] else: if array[i-1][j][0] > array[i][j-1][0] : array[i][j] = [array[i-1][j][0], array[i-1][j][1]] e.. 2021. 12. 25.
사다리 [백준 2022] - python def get_c(x, y, width) : h1 = (x**2 - width**2)**0.5 h2 = (y**2 - width**2)**0.5 c = h1 * h2 / (h1 + h2) return c def BOJ2022() : x, y, c = map(float, input().split()) start, end = 0, min(x,y) res = 0 while (end - start) > 0.000001 : width = (start + end) / 2 res = width if get_c(x, y, width) >= c : # c를 구했는데 기존의 c보다 같거나 크면 w값을 키워주어서 h1, h2의 값을 작게 해주어야 한다. start = width else : end = width print(.. 2021. 12. 23.
후위 표기식 2[백준 1935] - python import string def BOJ1935() : n = int(input()) quest = input() numbers = {} for i in string.ascii_uppercase[:n] : numbers[i] = float(input()) stack = [] ord = {'+': 1, '-': 1, '*': 2, '/': 2} for s in list(quest) : if s in ord : second = stack.pop() first = stack.pop() temp = 0 if s == '+' : temp = first + second elif s == '-' : temp = first - second elif s == '*': temp = first * second elif s =.. 2021. 12. 22.
다각형의 면적 [백준 2166] - python import sys import math input = sys.stdin.readline def BOJ2166() : N = int(input()) x_points = [] y_points = [] for _ in range(N) : x, y = map(int, input().split()) x_points.append(x) y_points.append(y) result = 0 for i in range(N) : if i == N-1 : result += x_points[i] * y_points[0] else : result += x_points[i] * y_points[i+1] for i in range(N): if i == N-1 : result -= x_points[0] * y_points[i] e.. 2021. 12. 21.
가장 긴 증가하는 부분 수열 4 [백준 14002] - python import sys from bisect import bisect_left input = sys.stdin.readline def BOJ14002(): N = int(input()) A = list(map(int, input().split())) dist = [-1000000001] index = [0] * (N + 1) for i in range(len(A)): num = A[i] if dist[-1] < num: dist.append(num) index[i] = len(dist) - 1 else: index[i] = bisect_left(dist, num) dist[index[i]] = num max_length = len(dist) - 1 print(max_length) answer = [] for.. 2021. 12. 20.