๐Ÿ—๏ธ Algorithm 344

๐ŸŸฉ [๋ฐฑ์ค€] [Python] [Silver5] 11004๋ฒˆ_K๋ฒˆ์งธ ์ˆ˜

๋ฌธ์ œ https://www.acmicpc.net/problem/11004 11004๋ฒˆ: K๋ฒˆ์งธ ์ˆ˜ ์ˆ˜ N๊ฐœ A1, A2, ..., AN์ด ์ฃผ์–ด์ง„๋‹ค. A๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ–ˆ์„ ๋•Œ, ์•ž์—์„œ๋ถ€ํ„ฐ K๋ฒˆ์งธ ์žˆ๋Š” ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. www.acmicpc.net ํ’€์ด # ๋ฐฑ์ค€ 11004๋ฒˆ ๋ฌธ์ œ - K๋ฒˆ์งธ ์ˆ˜ import sys input = sys.stdin.readline n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() print(a[k-1]) ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ํฌ๊ธฐ ๋•Œ๋ฌธ์— ๋ณ‘ํ•ฉ์ •๋ ฌ๋กœ ํ’€์–ด์•ผ ํ•œ๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ ๋‚˜๋Š” ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ•˜์˜€๋‹ค. ๋ณ‘ํ•ฉ์ •๋ ฌ์€ ๋‹ค์Œ ๋ฒˆ์— ๋‹ค๋ฃจ๋„๋ก ํ•ด์•ผ๊ฒ ๋‹ค.

โฌ› [Programmers] [Python] [Level2] ํ”ผ๋กœ๋„

๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/87946 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ํ’€์ด # ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2๋‹จ๊ณ„ - ํ”ผ๋กœ๋„ from itertools import permutations from collections import deque def solution(k, dungeons): answer = 0 for p in permutations(dungeons, len(dungeons)): tmp = k cnt = 0 for need, spend in p: if tmp >=..

โฌ› [Programmers] [Python] [Level3] ์ตœ๊ณ ์˜ ์ง‘ํ•ฉ

๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/12938 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ํ’€์ด # ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 3๋‹จ๊ณ„ - ์ตœ๊ณ ์˜ ์ง‘ํ•ฉ def solution(n, s): answer = [] if s < n: return [-1] for i in range(n): answer.append(s//n) idx = len(answer) - 1 for j in range(s % n): answer[idx] += 1 idx -= 1 return answer print(solution(2,..

โฌ› [Programmers] [Python] Level2_์—ฐ์† ๋ถ€๋ถ„ ์ˆ˜์—ด ํ•ฉ์˜ ๊ฐœ์ˆ˜

๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/131701 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ํ’€์ด # ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2๋‹จ๊ณ„ - ์—ฐ์† ๋ถ€๋ถ„ ์ˆ˜์—ด ํ•ฉ์˜ ๊ฐœ์ˆ˜ def solution(elements): result = set() elements_length = len(elements) elements = elements * 2 for i in range(elements_length): for j in range(elements_length): result.add(sum(elements[j..

โฌ› [Programmers] [Python] Level2_์œ„์žฅ

๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/42578 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ํ’€์ด # ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2๋‹จ๊ณ„ - ์œ„์žฅ def solution(clothes): answer = 1 dic = {} for v, k in clothes: if not k in dic: # ์ฒ˜์Œ์ผ ๊ฒฝ์šฐ ๋”•์…”๋„ˆ๋ฆฌ ์ดˆ๊ธฐํ™” dic[k] = 0 dic[k] += 1 for v in dic.values(): answer *= v + 1 return answer - 1

โฌ› [Programmers] [Python] Level2_๊ทค ๊ณ ๋ฅด๊ธฐ

๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/138476?language=python3 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ํ’€์ด # ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2๋‹จ๊ณ„ - ๊ทค ๊ณ ๋ฅด๊ธฐ from collections import Counter def solution(k, tangerine): answer = 0 d = sorted(Counter(tangerine).items(), key = lambda x : -x[1]) for key, value in d: if k

๐ŸŸฉ [๋ฐฑ์ค€] [Python] [Gold4] 15683๋ฒˆ_๊ฐ์‹œ

๋ฌธ์ œ https://www.acmicpc.net/problem/15683 15683๋ฒˆ: ๊ฐ์‹œ ์Šคํƒ€ํŠธ๋งํฌ์˜ ์‚ฌ๋ฌด์‹ค์€ 1×1ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋Š” N×M ํฌ๊ธฐ์˜ ์ง์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ๋ฌด์‹ค์—๋Š” ์ด K๊ฐœ์˜ CCTV๊ฐ€ ์„ค์น˜๋˜์–ด์ ธ ์žˆ๋Š”๋ฐ, CCTV๋Š” 5๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค. ๊ฐ CCTV๊ฐ€ ๊ฐ www.acmicpc.net ํ’€์ด # ๋ฐฑ์ค€ 15683๋ฒˆ ๋ฌธ์ œ - ๊ฐ์‹œ import sys; input = sys.stdin.readline import copy # CCTV ์ข…๋ฅ˜๋ณ„, ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉํ–ฅ๋ณ„ ๊ฐ์‹œ์˜์—ญ ์žฌ๊ท€์  ํƒ์ƒ‰ def dfs(graph, depth): global answer # ์ข…๋ฃŒ ์กฐ๊ฑด: ๋ชจ๋“  CCTV ํƒ์ƒ‰ if depth == len(cctv_list): # ์‚ฌ๊ฐ์ง€๋Œ€ ์ตœ์†Ÿ๊ฐ’ answer = mi..

๐ŸŸฉ [๋ฐฑ์ค€] [Python] [Gold4] 1976๋ฒˆ_์—ฌํ–‰ ๊ฐ€์ž

๋ฌธ์ œ https://www.acmicpc.net/problem/1976 1976๋ฒˆ: ์—ฌํ–‰ ๊ฐ€์ž ๋™ํ˜์ด๋Š” ์นœ๊ตฌ๋“ค๊ณผ ํ•จ๊ป˜ ์—ฌํ–‰์„ ๊ฐ€๋ ค๊ณ  ํ•œ๋‹ค. ํ•œ๊ตญ์—๋Š” ๋„์‹œ๊ฐ€ N๊ฐœ ์žˆ๊ณ  ์ž„์˜์˜ ๋‘ ๋„์‹œ ์‚ฌ์ด์— ๊ธธ์ด ์žˆ์„ ์ˆ˜๋„, ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๋™ํ˜์ด์˜ ์—ฌํ–‰ ์ผ์ •์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด ์—ฌํ–‰ ๊ฒฝ๋กœ๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ธ www.acmicpc.net ํ’€์ด # ๋ฐฑ์ค€ 1976๋ฒˆ ๋ฌธ์ œ - ์—ฌํ–‰ ๊ฐ€์ž import sys sys.setrecursionlimit(10 ** 6) # a๊ฐ€ ์†ํ•œ ์ง‘ํ•ฉ๊ณผ b๊ฐ€ ์†ํ•œ ์ง‘ํ•ฉ ํ•ฉ์น˜๊ธฐ def union(x, y): x = find(x) y = find(y) # a์™€ b์˜ ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ๊ฐ™์œผ๋ฉด ๋™์ผํ•œ ์ง‘ํ•ฉ์ด๋ฏ€๋กœ ๋ฆฌํ„ด if x == y: return # ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด ๋‘ ์ง‘ํ•ฉ์„ ํ•ฉ์นœ๋‹ค. else: parent..

๐ŸŸฉ [๋ฐฑ์ค€] [Python] [Gold4] 9935๋ฒˆ_๋ฌธ์ž์—ด ํญ๋ฐœ

๋ฌธ์ œ https://www.acmicpc.net/problem/9935 9935๋ฒˆ: ๋ฌธ์ž์—ด ํญ๋ฐœ ์ฒซ์งธ ์ค„์— ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 1,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋‘˜์งธ ์ค„์— ํญ๋ฐœ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๊ธธ์ด๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 36๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋‘ ๋ฌธ์ž์—ด์€ ๋ชจ www.acmicpc.net ํ’€์ด # ๋ฐฑ์ค€ 9935๋ฒˆ ๋ฌธ์ œ - ๋ฌธ์ž์—ด ํญ๋ฐœ import sys input = sys.stdin.readline # ์ž…๋ ฅ๊ฐ’ ์ฒ˜๋ฆฌ s = input().rstrip() bomb = input().rstrip() # stack์œผ๋กœ ๋ฌธ์ž์—ด ํญ๋ฐœ ๊ตฌํ˜„ stack = [] bomb_length = len(bomb) for char in s: stack.append(char) if ''..