Algorithm245 [백준] 1774번 - 우주신과의 교감 (파이썬) # 우주신과의 교감 import math import heapq import sys input = sys.stdin.readline N, M = map(int, input().split()) def prim(start): result = 0 visited = [False] * (N+1) q = [] heapq.heappush(q, (0, start)) while q: dist, node = heapq.heappop(q) if visited[node]: continue visited[node] = True result += dist for ndist, nnode in arr[node]: heapq.heappush(q, (ndist, nnode)) return result # 노드 위치 정보 nodes = .. 2022. 3. 15. [프로그래머스] Lv1 - 로또의 최고 순위와 최저 순위 (파이썬) https://programmers.co.kr/learn/courses/30/lessons/77484 코딩테스트 연습 - 로또의 최고 순위와 최저 순위 로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다. 1 순위 당첨 내용 1 6개 번호가 모두 일치 2 5개 번호 programmers.co.kr def solution(lottos, win_nums): minimum, maximum = 0, 0 for x in lottos: if x in win_nums: minimum += 1 minimum = 7 - minimum maximum = minimum - lottos.count(0) answer = [6 if .. 2022. 3. 15. [프로그래머스] Lv1 - 신고 결과 받기 (파이썬) https://programmers.co.kr/learn/courses/30/lessons/92334 코딩테스트 연습 - 신고 결과 받기 문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의 programmers.co.kr from collections import defaultdict def solution(id_list, reports, k): reported = dict() for report in reports: x, y = report.split(' ') if not reported.get(y): reported[y] = set([x]) else: reported[.. 2022. 3. 15. [백준] 17299번 - 오등큰수 (파이썬) # 오등큰수 from collections import Counter import sys input = sys.stdin.readline N = int(input()) arr = list(map(int, input().split())) count = Counter(arr) result = [-1] * N stack = [] for i in range(N): while stack and count[arr[stack[-1]]] < count[arr[i]]: result[stack.pop()] = arr[i] stack.append(i) print(*result) 자료구조(스택) 1. Count를 이용해서 개수 딕셔너리를 만든다. 2. 반복문 시작 - 현재 인덱스(stack [-1])의 값보다 i의 값이 더 .. 2022. 3. 14. [백준] 17298번 - 오큰수 (파이썬) # 오큰수 import sys input = sys.stdin.readline N = int(input()) arr = list(map(int, input().split())) result = [-1] * N stack = [] for i in range(N): while stack and arr[stack[-1]] < arr[i]: result[stack.pop()] = arr[i] stack.append(i) print(*result) 자료구조(스택) 1. stack = [0]인 상태로 시작 2. 반복문은 시작 - 현재 인덱스(stack [-1])의 값보다 i의 값이 더 작으면 전부 pop (pop한 인덱스의 답 : 현재 값) - 이후 스택에 i appepnd 3. result 출력 스택 내부를 항상.. 2022. 3. 14. [프로그래머스] 고득점Kit (10) - 그래프 (파이썬) 1. 가장 먼 노드 (Lv. 3) # 가장 먼 노드 import sys MAX = sys.maxsize def solution(n, edge): arr = [[] for _ in range(n+1)] for x, y in edge: arr[x].append(y) arr[y].append(x) return go(n, arr) def go(n, arr): visited = [MAX] * (n+1) visited[0], visited[1] = 0, 1 q = [] q.append(1) while q: node = q.pop() for nnode in arr[node]: if visited[nnode] > visited[node] + 1: q.append(nnode) visited[nnode] = visited.. 2022. 3. 13. [백준] 6497번 - 전력난 (파이썬) # 전력난 import heapq import sys input = sys.stdin.readline def prim(road_info, house, start): result = 0 q = [] heapq.heappush(q, (0, start)) visited = [False] * house while q: cost, home = heapq.heappop(q) if visited[home]: continue result += cost visited[home] = True for ncost, nhome in road_info[home]: if not visited[nhome]: heapq.heappush(q, (ncost, nhome)) return result while True: house, roa.. 2022. 3. 13. 이전 1 ··· 16 17 18 19 20 21 22 ··· 35 다음