practivceAlgorithm/백준 379

[백준][Python] 3273 두 수의 합

동일한 수가 나오는 경우를 대비했는데 1등코드보니 같은수는 안나오는 것 같다. 일단 counting한 후 set에 저장한 것에서 checking하며 각 조합의 수를 answer 에 더해줬다. import sys input = sys.stdin.readline from collections import defaultdict from itertools import combinations n = int(input()) arr = list(map(int, input().split())) x = int(input()) check = defaultdict(int) nums = set() for num in arr: check[num] += 1 nums.add(num) answer = 0 for num in nums..

[백준][Python] 2665 미로만들기

가중치가 있는 사방탐색. 흔한 문제들처럼 거리 가중치가 1인 경로를 찾는 것이 아닌 벽을 만나는 갯수를 가중치가 있는간선, 없는 간선으로 두어 우선처리해주어야 한다. heap을 쓰는 다익스트라, deque의 appendleft를 쓰는 dfs 두가지 방법이 있다. import sys input = sys.stdin.readline from heapq import heappush, heappop def dijkstra(): heap = [] heappush(heap, [0, 0, 0]) visited[0][0] = 1 while heap: cost, x, y = heappop(heap) if x == n - 1 and y == n - 1: return cost for i in range(4): nx = x ..

[백준][Python] 1713 후보 추천하기

사진틀을 dict로 관리 갯수 초과하면 하나 삭제 import sys input = sys.stdin.readline from collections import defaultdict N = int(input()) n = int(input()) nums = list(map(int, input().split())) count = defaultdict(int) for i in range(n): count[nums[i]] += 1 if len(count) > N: min_val = float('inf') for candidate in count: if candidate == nums[i]: continue if count[candidate] < min_val: min_val = count[candidate] t..

[백준][Python] 5766 할아버지는 유명해

1등을 찾고 삭제한 그룹에서 1등을 찾으면 2등 그룹이 나온다. import sys input = sys.stdin.readline from collections import defaultdict while 1: N, M = map(int, input().split()) if not N and not M: break rankers = defaultdict(int) answer = [] max_cnt = 0 for _ in range(N): a = list(map(int, input().split())) for num in a: rankers[num] += 1 if rankers[num] > max_cnt: max_cnt = rankers[num] answer = [num] elif rankers[num]..

[백준][Python] 19238 스타트택시

bfs로 승객을 찾고 bfs로 도착지로 이동하는 방식. 도착지 이동하면 oil 충전하고 탑승요청 삭제. 유의할 예외는 도착지점 or 시작지점이 다른 승객 위치랑 겹치는 경우도 고려해주어야한다. import sys input = sys.stdin.readline from collections import deque from heapq import heappush def go_target(start_x, start_y, start_oil): q = deque() q.append((start_x, start_y, start_oil)) visited = [[False] * N for __ in range(N)] visited[start_x][start_y] = True while q: for __ in rang..