본문 바로가기

반응형

전체 글

(141)
백준 1715번: 카드 정렬하기 파이썬 코드(우선순위 큐) #1715 import heapq import sys n = int(sys.stdin.readline()) heap = [] carculate = [] for _ in range(n): card = int(sys.stdin.readline()) heapq.heappush(heap, card) while len(heap) > 1: temp1 = heapq.heappop(heap) temp2 = heapq.heappop(heap) carculate.append(temp1 + temp2) heapq.heappush(heap,temp1 + temp2) print(sum(carculate)) 설명 숫자끼리 더하면서 생기는 연산을 모았다가 더하면 되는 문제이다. 다만 적은 수끼리 더하는 것이 연산이 적기 때문에, ..
백준 11000번: 강의실 배정 파이썬 코드(우선순위 큐) import heapq import sys n = int(sys.stdin.readline()) heap = [] q = [] count = 0 for _ in range(n): start, end = map(int,sys.stdin.readline().split()) heapq.heappush(heap, [start,end]) start, end = heapq.heappop(heap) heapq.heappush(q, end) while heap: start, end = heapq.heappop(heap) if q[0]
백준 1374번: 강의실 파이썬 코드(우선순위 큐) import heapq import sys n = int(sys.stdin.readline()) heap = [] q = [] count = 0 for _ in range(n): num, start, end = map(int,sys.stdin.readline().split()) heapq.heappush(heap, [start,end,num]) start, end, num = heapq.heappop(heap) heapq.heappush(q, end) while heap: start, end, num = heapq.heappop(heap) if q[0]
백준 1342번: 행운의 문자열 파이썬코드(백트래킹) s = input() s = list(s) su = len(s) count = 0 alphabet = [0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0] for i in range(len(s)): alphabet[ord(str(s[i])) - 97] += 1 def backtracking(l, p): global count if l == 0: count += 1 for i in range(26): if alphabet[i] > 0 and i != p: alphabet[i] -= 1 backtracking(l-1, i) alphabet[i] += 1 else: pass backtracking(su, -1) print(count)
백준 1717번: 집합의 표현 파이썬 코드(유니온파인드, Union-find) import sys n, m = map(int, sys.stdin.readline().split()) parent = [i for i in range(n+1)] def find(x): if parent[x] == x: return x return find(parent[x]) def union(x, y): x = find(x) y = find(y) if x != y: if y > x: parent[y] = x else: parent[x] = y return def same(x, y): x = find(x) y = find(y) if x == y: print("YES") else: print("NO") return for i in range(m): a,b,c = map(int, sys.stdin.readli..
백준 4485번: 녹색 옷 입은 애가 젤다지? 파이썬 코드(다익스트라) import sys import heapq INF = sys.maxsize x = [0,0,-1,1] y = [-1,1,0,0] count = 0 while 1: n = int(sys.stdin.readline()) count += 1 visited = [[0 for _ in range(n)]for _ in range(n)] if n == 0: break graph = [] dist = [[INF for _ in range(n)] for _ in range(n)] for i in range(n): temp = list(map(int, sys.stdin.readline().split())) graph.append(temp) heap = [] heapq.heappush(heap, [graph[0][0],[0..
백준 1261번: 알고스팟 파이썬 코드(다익스트라) import sys import heapq INF = sys.maxsize x = [0,0,-1,1] y = [-1,1,0,0] n,m = map(int, sys.stdin.readline().split()) visited = [[0 for _ in range(n)]for _ in range(m)] graph = [] dist = [[INF for _ in range(n)] for _ in range(m)] for i in range(m): temp = list(input()) temp = [int(i) for i in temp] graph.append(temp) heap = [] heapq.heappush(heap, [graph[0][0],[0,0]]) while heap: cost, now = hea..
파이썬 투포인터 알고리즘 사용하기(Two pointer) 투포인터 알고리즘? 투포인터 알고리즘은 이름에서 알 수 있듯 2개의 포인터를 사용하는 알고리즘이다. 슬라이딩 윈도우 알고리즘과도 많이 비교가 되는데, 그 이유는 밑에서 설명하겠다. 투포인터 알고리즘은 배열의 인덱스를 가르키는 2개의 포인터를 설정하고, 이를 옮겨가면서 내가 원하는 값을 찾는 알고리즘이다. 투포인터는 간단하기도 해서 그냥 바로 예시 상황을 보고 이해하는게 빠를 것이다. 상황 1. 길이가 5인 배열이 하나 있다. 이 안에는 임의의 10미만의 정수가 할당되어있다. 2. 이 배열에서 어느 한 부분을 잘라서 그 부분의 합이 15가 되는 부분을 찾고 출력하라. 1 4 2 5 3 6 순서로 보시면 된다. (S = 보라색포인터와 파란색포인터 사이의 값의 합) 1. 보라색 포인터와 파란색 포인터가 둘다 ..

반응형