Algorithm

Algorithm/백준(파이썬)

백준 2470 파이썬(Python) 문제풀이 두 용액

문제링크:https://www.acmicpc.net/problem/2470 import sys N = int(sys.stdin.readline()) Liquid = list(map(int, sys.stdin.readline().split())) Liquid.sort() start = 0 end = len(Liquid) - 1 mix = sys.maxsize # 최솟값 변수 초기화 while start < end: newmix = Liquid[start] + Liquid[end] if abs(newmix) < mix: # 기존의 mix값보다 new mix의 절대값이 작을떄 mix = abs(newmix) ans = [Liquid[start], Liquid[end]] if newmix < 0: # 0보다 작..

Algorithm/백준(파이썬)

백준 2110 파이썬(Python) 문제풀이 공유기 설치

문제링크:https://www.acmicpc.net/problem/2110 import sys N, C = map(int, sys.stdin.readline().split()) home = [] for _ in range(N): home.append(int(sys.stdin.readline())) home.sort() # 이분 탐색을 위해 정렬 start = 0 end = home[-1] ans = 0 def binary(arr, s, e): while s = current + m: # arr[i]보다 작거나 같으면 count +=1 공유기를 arr[i]에 설치 count += 1 current = arr[i] if count >= C: # C를 넘어가면 더 넓게 설치할수있다는 말 => s = m+1 g..

Algorithm/백준(파이썬)

백준 10971 파이썬(Python) 문제풀이 외판원순회 2

문제링크:https://www.acmicpc.net/problem/10971 import sys input = sys.stdin.readline n = int(input()) costs = [list(map(int, input().split())) for _ in range(n)] visited = [0] * n # 방문했는지 안했는지 sum_cost = 0 # 방문비용 합 ans = sys.maxsize def dfs(depth, x): global sum_cost, ans if depth == n - 1: # 종료 조건 if costs[x][0]: sum_cost += costs[x][0] if sum_cost < ans: # 최소값 비교 ans = sum_cost sum_cost -= costs[x..

Algorithm/백준(파이썬)

백준 10819 파이썬(Python) 문제풀이 차이를 최대로

문제링크:https://www.acmicpc.net/problem/10819 import sys import itertools # N개의 정수로 이루어진 리스트 입력받기 N = int(sys.stdin.readline()) A = list(map(int, sys.stdin.readline().split())) # permutations 메소드는 가능한 순열들의 조합을 튜플로 생성해 해당 레퍼런스를 반환한다. per = itertools.permutations(A) res = 0 for i in per: s = 0 # per안의 순열들의 차이(s)를 구해 s가 res보다 클시에 업데이트 for j in range(1, N): s += abs(i[j - 1] - i[j]) if res < s: res = s..

Algorithm/백준(파이썬)

백준 1074 파이썬(Python) 문제풀이 Z

문제링크:https://www.acmicpc.net/problem/1074 import sys def z(n, r, c, res): length = 2 ** n # 배열 길이 half = length // 2 # 사분면으로 만들기위한 배열길이의 절반길이 구하기 if n == 1: # 재귀 종료조건 2x2 4 최소 사분면이 만들어질 경우 print(2 * r + c + res) # row가 늘어나면 도착횟수가 2씩 늘어나고 colunm이 늘어나면 1씩 늘어난다. return if r >= half and c >= half: # 4사분면일떄 조건 z(n - 1, r - half, c - half, res + 3 * half * half) elif r >= half > c: # 3사분면일때 조건 z(n - 1..

Algorithm/백준(파이썬)

백준 9663 파이썬(Python) 문제풀이 N-Queen(N퀸)

문제링크:https://www.acmicpc.net/problem/9663 # limitation - 백트레킹 함수 > 퀸이 같은 행렬에 있으면 안됨, 퀸이 대각선에 있으면 안됨 false일시 그행 제외 # 입력 N은 NxN 체스판 row[0]*n 으로 사용 # result - > =+ 로 출력할 방법갯수 카운팅 # 출력 퀸을 놓을수있는 방법 출력 # import sys n = int(sys.stdin.readline()) row = [0] * n cnt = 0 #퀸을 놓고 그 이후 놓기 불가능한 줄 찾기 def prommising(a): for i in range(a): if row[a] == row[i] or abs(row[a] - row[i]) == abs(a - i): return False re..

Algorithm/백준(파이썬)

백준 1914 파이썬(Python) 문제풀이 하노이의 탑

문제링크:https://www.acmicpc.net/problem/1914 import sys n = int(sys.stdin.readline()) # n: 원판 갯수 , 1번기둥(start) - s기둥, 2번 기둥(middle) - m 기둥, 3번 기둥(end) - e 기둥 def hanoi(n, s, e): m = 6 - s - e # 2번기둥 if n == 1: print(s, e) # else: hanoi(n - 1, s, m) # n-1 의 원판을 전부 s -> m (그 안에서 print 1,3 -> 1,2 -> 3,2...) print(s, e) #남은 하나를 s -> e 1,3 hanoi(n - 1, m, e) # m에 위치한 n-1의 원판을 전부 -> e로 print(2 ** n - 1) ..

Algorithm/백준(파이썬)

백준 2628 파이썬(Python) 문제풀이 종이자르기

문제링크:https://www.acmicpc.net/problem/2628 import sys paper = list(map(int, sys.stdin.readline().split())) # paper 는 종이 가로 세로 입력 t = int(sys.stdin.readline()) width = [0, paper[0]] # width는 가로 길이 *미리 0이 들어가있는 것이 중요!!* height = [0, paper[1]] # height는 세로 길이 *미리 0이 들어가있는 것이 중요!!* #이유 -> 종이를 xy로 생각했을떄 0,0 좌표가 있어야 최대 넓이값 구할떄 오류 발생 x for _ in range(t): # t는 테스트 케이스 수 c = list(map(int, sys.stdin.readli..

김상주
'Algorithm' 카테고리의 글 목록 (5 Page)