Algorithm
-
백준 3273 두 수의 합 파이썬Algorithm/Coding test 2022. 8. 14. 23:41
import sys N = int(input()) nlist = list(map(int, sys.stdin.readline().split())) X = int(input()) b = {} for n in nlist: b[n] = False a = [] cnt = 0 for n in nlist: if X - n == n: a.append(n) continue try: c = b[X-n] except KeyError: continue if c is False: b[X-n] = True b[n] = True cnt += 1 print(cnt) 딕셔너리 활용하기 좋은 문제였다. 처음에는 배열의 모든 값을 확인하는 코드를 사용했다. (list) import sys N = int(input()) nlist = li..
-
백준 21608 상어 초등학교 파이썬Algorithm/Coding test 2022. 8. 7. 12:28
import sys N = int(input()) students = list(list(map(int, sys.stdin.readline().split())) for _ in range(N * N)) classroom = [[0] * N for _ in range(N)] # 최종 정해진 자리 dir = [(1, 0), (0, 1), (-1, 0), (0, -1)] fixed = [] # 정해진 자리 (다른 학생을 앉힐 수 없는 자리) # 첫 번째 조건 : 좋아하는 학생이 주위에 가장 많은 자리 선택 def condition1(like): maxf = 0 # 주위 좋아하는 학생 수 best = [] # 자리 후보 (조건 2로 넘기기 위해 list로 설정) # 모든 자리 확인 for i in range(N)..
-
눈물나는 백준 1325 효율적인 해킹Algorithm/Coding test 2022. 7. 23. 22:25
from collections import deque import sys def bfs(graph, start, N): flag = [False] * (N + 1) queue = deque([start]) flag[start] = True cnt = 1 while(queue): v = queue.popleft() for i in graph[v]: if not flag[i]: queue.append(i) flag[i] = True cnt += 1 return cnt N, M = map(int, input().split()) arr = [[] for _ in range(N+1)] for i in range(M): a, b = map(int, sys.stdin.readline().split()) arr[b]...