목록python (182)
rose_brown
1. 문제https://school.programmers.co.kr/learn/courses/30/lessons/72413 2. 코드python 1import sysimport heapqdef dijkstra(s, graph, n): dist = [sys.maxsize] * (n+1) q = [(0, s)] dist[s] = 0 while q: cost, now = heapq.heappop(q) if dist[now] 풀이연결된 노드의 최단 거리를 찾는 것 → 다익스트라 생각함fares가 어떻게 연결 되었는지를 위해 graph생성양항향으로 연결된 node와 값을 저장다익스트라 함수거리 배열 dist: n+1 크기로 생성 후 모두 무한대로 초기화시작 노..

1. 문제https://www.acmicpc.net/problem/20056 2. 코드python 1import sysinput = sys.stdin.readlineN, M, K = map(int, input().split())fireballs = []for i in range(M): r, c, m, s, d = map(int, input().split()) fireballs.append((r-1, c-1, m, s, d))# 8방향dr = [-1, -1, 0, 1, 1, 1, 0, -1]dc = [0, 1, 1, 1, 0, -1, -1, -1]def move(fireballs): grid = [[[] for _ in range(N)] for _ in range(N)] for r, ..
1. 문제https://school.programmers.co.kr/learn/courses/30/lessons/84021 2. 코드python 1from collections import dequedef bfs(x, y, visited, board, target): queue = deque() queue.append((x, y)) visited[x][y] = True shape = [(x, y)] directions = [(-1,0), (1,0), (0,-1), (0,1)] while queue: cx, cy = queue.popleft() for dx, dy in directions: nx, ny = cx+dx, cy+dy ..

1. 문제https://www.acmicpc.net/problem/11000 2. 코드python 1import sysimport heapqinput = sys.stdin.readlinetime = []heap = []N = int(input())for _ in range(N): time.append(list(map(int, input().split())))time.sort(key = lambda x : (x[0], x[1]))for start, end in time: if heap and heap[0] 풀이강의 개수 N과 각 강의의 시작 시간, 종료 시간을 입력받음강의들을 시작 시간이 빠른 순으로 정렬우선순위 큐(min-heap) 를 만들어, 각 강의의 종료 시간을 저장모든 강의에 대해:현재..

1. 문제https://www.acmicpc.net/problem/3190 2. 코드python 1import sysfrom collections import dequeinput = sys.stdin.readlineN = int(input())K = int(input())graph = [[0 for _ in range(N+1)] for _ in range(N+1)]for _ in range(K): x, y = map(int, input().split()) graph[x][y] = 1L = int(input())directions = dict()for _ in range(L): t, d = input().split() directions[int(t)] = ddx = [0, 1, 0, ..

1. 문제https://www.acmicpc.net/problem/2668 2. 코드python 1import sysinput = sys.stdin.readlineN = int(input())arr = [0] * (N+1)visited = [False] * (N+1)answer = set()def DFS(start, current, path): visited[current] = True path.append(current) next_node = arr[current] if not visited[next_node]: DFS(start, next_node, path) elif next_node == start: answer.update(path) vi..

1. 문제https://www.acmicpc.net/problem/17413 2. 코드python 1import sysinput = sys.stdin.readlineS = input().rstrip()stack = []answer = ''tag = Falsefor s in S: if s == '': tag = False answer += s elif tag: answer += s else: if s.isalnum(): stack.append(s) elif s == ' ': while stack: answer += stack.pop() answ..

1. 문제https://www.acmicpc.net/problem/2606 2. 코드python 1import sysinput = sys.stdin.readlinedef dfs(idx): global visited, graph, answer visited[idx] = True answer += 1 for i in range(1, N + 1): if not visited[i] and graph[idx][i]: dfs(i)N = int(input())M = int(input())graph = [[False] * (N + 1) for _ in range(N + 1)]visited = [False] * (N + 1)answer = 0for _ in range..

1. 문제https://www.acmicpc.net/problem/20920 2. 코드python 1import sysfrom collections import defaultdictinput = sys.stdin.readlineN, M = map(int, input().split())word_list = defaultdict(int)for n in range(N): word = input().strip() if len(word) >= M: word_list[word] += 1answer = sorted(word_list.items(), key=lambda x:(-x[1],-len(x[0]),x[0]))for key, item in answer: print(key)풀이N - ..

1. 문제https://www.acmicpc.net/problem/13414 2. 코드python 1import sysinput = sys.stdin.readlineK, L = map(int, input().split())dic = {}for i in range(L): dic[input().strip()] = ianswer = sorted(dic.items(), key= lambda x:x[1])for k in range(min(K, len(answer))): print(answer[k][0])풀이K, L 값을 받음중복된 학번이라면 나중에 넣은 값이 저장 되어야 함으로 dict에 넣어서 → 중복 접속한 경우 가장 마지막 번째를 저장저장된 dic에서 접속한 순서대로 정렬k명만 들을 수 있으니까 ..