๐Ÿ—๏ธ Algorithm 344

๐ŸŸฉ [๋ฐฑ์ค€] [Python] [Silver3] 15903๋ฒˆ_์นด๋“œ ํ•ฉ์ฒด ๋†€์ด

๋ฌธ์ œ https://www.acmicpc.net/problem/15903 15903๋ฒˆ: ์นด๋“œ ํ•ฉ์ฒด ๋†€์ด ์ฒซ ๋ฒˆ์งธ ์ค„์— ์นด๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆ˜ n(2 ≤ n ≤ 1,000)๊ณผ ์นด๋“œ ํ•ฉ์ฒด๋ฅผ ๋ช‡ ๋ฒˆ ํ•˜๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆ˜ m(0 ≤ m ≤ 15×n)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„์— ๋งจ ์ฒ˜์Œ ์นด๋“œ์˜ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” n๊ฐœ์˜ ์ž์—ฐ์ˆ˜ a1, www.acmicpc.net ํ’€์ด # ๋ฐฑ์ค€ 15903๋ฒˆ ๋ฌธ์ œ - ์นด๋“œ ํ•ฉ์ฒด ๋†€์ด import sys import heapq input = sys.stdin.readline n, m = map(int, input().split()) cards = list(map(int, input().split())) heapq.heapify(cards) for i in range(m): value ..

๐ŸŸฉ [๋ฐฑ์ค€] [Python] [Silver3] 2346๋ฒˆ_ํ’์„  ํ„ฐ๋œจ๋ฆฌ๊ธฐ

๋ฌธ์ œ https://www.acmicpc.net/problem/2346 2346๋ฒˆ: ํ’์„  ํ„ฐ๋œจ๋ฆฌ๊ธฐ 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ N๊ฐœ์˜ ํ’์„ ์ด ์›ํ˜•์œผ๋กœ ๋†“์—ฌ ์žˆ๊ณ . i๋ฒˆ ํ’์„ ์˜ ์˜ค๋ฅธ์ชฝ์—๋Š” i+1๋ฒˆ ํ’์„ ์ด ์žˆ๊ณ , ์™ผ์ชฝ์—๋Š” i-1๋ฒˆ ํ’์„ ์ด ์žˆ๋‹ค. ๋‹จ, 1๋ฒˆ ํ’์„ ์˜ ์™ผ์ชฝ์— N๋ฒˆ ํ’์„ ์ด ์žˆ๊ณ , N๋ฒˆ ํ’์„ ์˜ ์˜ค๋ฅธ์ชฝ์— 1๋ฒˆ ํ’์„  www.acmicpc.net ํ’€์ด # ๋ฐฑ์ค€ 2346๋ฒˆ ๋ฌธ์ œ - ํ’์„  ํ„ฐ๋œจ๋ฆฌ๊ธฐ import sys from collections import deque input = sys.stdin.readline n = int(input()) q = deque(enumerate(map(int, input().split()))) answer = [] while q: idx, paper = q.popleft() answ..

๐ŸŸฉ [๋ฐฑ์ค€] [Python] [Gold4] 1339๋ฒˆ_๋‹จ์–ด ์ˆ˜ํ•™

๋ฌธ์ œ https://www.acmicpc.net/problem/1339 1339๋ฒˆ: ๋‹จ์–ด ์ˆ˜ํ•™ ์ฒซ์งธ ์ค„์— ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 10)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๋‹จ์–ด๊ฐ€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ๋‹จ์–ด๋Š” ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ์žˆ๋‹ค. ๋ชจ๋“  ๋‹จ์–ด์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ์•ŒํŒŒ๋ฒณ์€ ์ตœ๋Œ€ www.acmicpc.net ํ’€์ด n = int(input()) words = [] for _ in range(n): words.append(list(map(lambda x: ord(x) - 65, input()))) # ์•ŒํŒŒ๋ฒณ์„ ์ˆซ์ž๋กœ ๋ฐ”๊พธ์–ด ์ธ๋ฑ์Šค๋กœ ์“ธ ์ˆ˜ ์žˆ๋„๋ก ํ•จ alphabets = [0] * 26 # ๊ฐ ์•ŒํŒŒ๋ฒณ์˜ ์ž๋ฆฟ์ˆ˜ ์ €์žฅ for word in words: for idx, char in enumerat..

๐ŸŸฉ [๋ฐฑ์ค€] [Python] [Gold5] 13219๋ฒˆ_Trains

๋ฌธ์ œ https://www.acmicpc.net/short/status/13219/1 13219๋ฒˆ ์ˆ์ฝ”๋”ฉ - 1 ํŽ˜์ด์ง€ ๋ชจ๋“  ์–ธ์–ดC++JavaPythonCRustC++17Java 8Python 3C11PyPy3C99C++98C++11C++14Java 8 (OpenJDK)Java 11C++20RubyKotlin (JVM)SwiftTextC#node.jsGoGo (gccgo)Java 15DD (LDC)PHPRust 2015Rust 2018Rust 2021PascalScalaLuaPerlF#Visual BasicObjective-CObjective-C++C99 ( www.acmicpc.net ํ’€์ด # ๋ฐฑ์ค€ 13219๋ฒˆ ๋ฌธ์ œ - trains from heapq import heappush, heappop imp..

๐ŸŸฉ [๋ฐฑ์ค€] [Python] [Gold2] 12015๋ฒˆ_๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด 2

๋ฌธ์ œ https://www.acmicpc.net/problem/12015 12015๋ฒˆ: ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด 2 ์ฒซ์งธ ์ค„์— ์ˆ˜์—ด A์˜ ํฌ๊ธฐ N (1 ≤ N ≤ 1,000,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ˆ˜์—ด A๋ฅผ ์ด๋ฃจ๊ณ  ์žˆ๋Š” Ai๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ Ai ≤ 1,000,000) www.acmicpc.net ํ’€์ด # ๋ฐฑ์ค€ 12015๋ฒˆ ๋ฌธ์ œ - ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด n = int(input()) arr = list(map(int, input().split())) stack = [0] def binary_search(target, start, end): while start

๐ŸŸฉ [๋ฐฑ์ค€] [Python] [Gold4] 1043๋ฒˆ_๊ฑฐ์ง“๋ง

๋ฌธ์ œ https://www.acmicpc.net/problem/1043 1043๋ฒˆ: ๊ฑฐ์ง“๋ง ์ง€๋ฏผ์ด๋Š” ํŒŒํ‹ฐ์— ๊ฐ€์„œ ์ด์•ผ๊ธฐ ํ•˜๋Š” ๊ฒƒ์„ ์ข‹์•„ํ•œ๋‹ค. ํŒŒํ‹ฐ์— ๊ฐˆ ๋•Œ๋งˆ๋‹ค, ์ง€๋ฏผ์ด๋Š” ์ง€๋ฏผ์ด๊ฐ€ ๊ฐ€์žฅ ์ข‹์•„ํ•˜๋Š” ์ด์•ผ๊ธฐ๋ฅผ ํ•œ๋‹ค. ์ง€๋ฏผ์ด๋Š” ๊ทธ ์ด์•ผ๊ธฐ๋ฅผ ๋งํ•  ๋•Œ, ์žˆ๋Š” ๊ทธ๋Œ€๋กœ ์ง„์‹ค๋กœ ๋งํ•˜๊ฑฐ๋‚˜ ์—„์ฒญ๋‚˜๊ฒŒ www.acmicpc.net ํ’€์ด # ๋ฐฑ์ค€ 1043๋ฒˆ ๋ฌธ์ œ - ๊ฑฐ์ง“๋ง import sys n, m = list(map(int, sys.stdin.readline().split())) truth = list(map(int, sys.stdin.readline().split()))[1:] KNOW_TRUTH = 0 parent = [i for i in range(n + 1)] for person in truth: parent[perso..

๐ŸŸฉ [๋ฐฑ์ค€] [Python] [Gold5] 13023๋ฒˆ_ABCDE

๋ฌธ์ œ https://www.acmicpc.net/problem/13023 13023๋ฒˆ: ABCDE ๋ฌธ์ œ์˜ ์กฐ๊ฑด์— ๋งž๋Š” A, B, C, D, E๊ฐ€ ์กด์žฌํ•˜๋ฉด 1์„ ์—†์œผ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค. www.acmicpc.net ํ’€์ด # ๋ฐฑ์ค€ 13023๋ฒˆ ๋ฌธ์ œ - ABCDE # dfs import sys input = sys.stdin.readline sys.setrecursionlimit(100000) n, m = map(int , input().split()) visited = [0]*(n) graph = [ [] for _ in range(n) ] answer = 0 for _ in range(m): a,b = map(int, input().split()) graph[a].append(b) graph[b].appen..

โฌ› [Programmers] [Python] [Level3] ์—ฌํ–‰ ๊ฒฝ๋กœ

๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/43164/solution_groups?language=python3 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ํ’€์ด # ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 3๋‹จ๊ณ„ - ์—ฌํ–‰ ๊ฒฝ๋กœ from collections import defaultdict def solution(tickets): path = [] graph = defaultdict(list) for (start, end) in tickets: graph[start].append(end) for airport in..

โฌ› [Programmers] [Python] [Level2] ์นด๋“œ ๋ญ‰์น˜

๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/1844 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ํ’€์ด # ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2๋‹จ๊ฒŒ - ๊ฒŒ์ž„ ๋งต ์ตœ๋‹จ๊ฑฐ๋ฆฌ from collections import deque def solution(maps): n = len(maps) m = len(maps[0]) visited = [[0]*m for _ in range(n)] q = deque() q.append((0,0)) dx, dy = [0,0,-1,1], [1,-1,0,0] visited[0][0] = ..

๐ŸŸฉ [๋ฐฑ์ค€] [Python] [Gold3] 1238๋ฒˆ_ํŒŒํ‹ฐ

๋ฌธ์ œ https://www.acmicpc.net/problem/1238 1238๋ฒˆ: ํŒŒํ‹ฐ ์ฒซ์งธ ์ค„์— N(1 ≤ N ≤ 1,000), M(1 ≤ M ≤ 10,000), X๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ž…๋ ฅ๋œ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ M+1๋ฒˆ์งธ ์ค„๊นŒ์ง€ i๋ฒˆ์งธ ๋„๋กœ์˜ ์‹œ์ž‘์ , ๋์ , ๊ทธ๋ฆฌ๊ณ  ์ด ๋„๋กœ๋ฅผ ์ง€๋‚˜๋Š”๋ฐ ํ•„์š”ํ•œ ์†Œ์š”์‹œ๊ฐ„ Ti๊ฐ€ ๋“ค์–ด www.acmicpc.net ํ’€์ด # ๋ฐฑ์ค€ 1238๋ฒˆ ๋ฌธ์ œ - ํŒŒํ‹ฐ import heapq n, m, x = map(int, input().split()) # n: ๋…ธ๋“œ, m: ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ # ๋ชจ๋“  ํ•™์ƒ x์— ๊ฐˆ ์ˆ˜ ์žˆ๊ณ  x์—์„œ ์ง‘์œผ๋กœ ๋Œ์•„์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ž…๋ ฅ graph = [[] for _ in range(n+1)] for _ in range(m): a, b, cost = map(i..