๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm

(120)
[Programmers] ํŒŒ์ผ๋ช… ์ •๋ ฌ ํ•ด์„ค ์ฝ”๋“œ def solution(files): ans = [] head, number, tail = '','','' for file in files: for i in range(len(file)): # ์šฐ์„  ํŒŒ์ผ์„ head์™€ number๋กœ๋งŒ ๋‚˜๋ˆˆ๋‹ค. if file[i].isdigit(): head = file[:i] number = file[i:] # number์„ ๋‹ค์‹œ number์™€ tail๋กœ ๋‚˜๋ˆˆ๋‹ค. for k in range(len(number)): if not number[k].isdigit(): tail = number[k:] number = number[:k] break # ๋ฐฐ์—ด์— ๊ฐ๊ฐ์˜ ๊ฐ’์„ ์ถ”๊ฐ€ํ•œ ํ›„ head,number,tail๊ฐ’์„ resetํ•œ๋‹ค. ans.append([head,numb..
[BOJ] ๊ตญํšŒ์˜์› ์„ ๊ฑฐ(#1417) ์ฝ”๋“œ n = int(input()) dasom = int(input()) arr = [int(input()) for _ in range(n-1)] cnt = 0 if n == 1: pass else: while dasom ์ฒ˜์Œ์— ๋‚˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ๋ชจ๋“  ํ›„๋ณด์ž์˜ ๋“ํ‘œ์ˆ˜์—์„œ ํ•˜๋‚˜์”ฉ ๋นผ์„œ ๋‹ค์†œ์ด์˜ ๋“ํ‘œ์ˆ˜์— ๋”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜์˜€๋Š”๋ฐ, ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ๋ฌธ์ œ์—์„œ ์ˆœ์„œ๋Œ€๋กœ ์‚ฌ๋žŒ์„ ๋งค์ˆ˜ํ•˜๋ผ๊ณ  ์ ํ˜€ ์žˆ์ง€ ์•Š์•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ๊ทธ๋ƒฅ ์ตœ๋‹ค๋“ํ‘œ์ž์˜ ๋“ํ‘œ์ˆ˜๋ฅผ ๊ณ„์† ๋นผ์„œ ์ตœ๋‹ค ๋“ํ‘œ์ž๊ฐ€ ๋‹ค์†œ์ด ๋  ๋•Œ๊นŒ์ง€ ์‚ฌ๋žŒ์„ ๋งค์ˆ˜ํ•˜๋ฉด ํ•ด๊ฒฐ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. 1417๋ฒˆ: ๊ตญํšŒ์˜์› ์„ ๊ฑฐ ์ฒซ์งธ ์ค„์— ํ›„๋ณด์˜ ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๊ธฐํ˜ธ 1๋ฒˆ์„ ์ฐ์œผ๋ ค๊ณ  ํ•˜๋Š” ์‚ฌ๋žŒ์˜ ์ˆ˜, ๊ธฐํ˜ธ 2๋ฒˆ์„ ์ฐ์œผ๋ ค๊ณ  ํ•˜๋Š” ์ˆ˜, ์ด๋ ‡๊ฒŒ ์ด N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ..
[BOJ] ํ•œ๊ตญ์ด ๊ทธ๋ฆฌ์šธ ๋• ์„œ๋ฒ„์— ์ ‘์†ํ•˜์ง€ ์ฝ”๋“œ t = int(input()) imp = input().split('*') for _ in range(t): word = input() if len(word) >= len(imp[0]) + len(imp[1]): if word[0:len(imp[0])] == imp[0] and word[-1 * len(imp[1]):] == imp[1]: print("DA") else: print('NE') else: print('NE') ๋ถ„์„ > ๊ฑฐ์˜ ๋‹ค ํ’€์—ˆ๋Š”๋ฐ, imp[0]๊ณผ imp[1]์˜ ๊ธธ์ด๋ฅผ ๋”ํ–ˆ์„ ๋•Œ word์˜ ๊ธธ์ด๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ๋ฅผ ๋†“์ณ์„œ ๊ณ„์† ๋ฌธ์ œ๋ฅผ ํ’€์ง€ ๋ชปํ•˜์˜€๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด, ab*bc ์ด๊ณ  word๊ฐ€ abc์ธ ๊ฒฝ์šฐ ์ด๋Š” ์˜ณ์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๋‚˜๋Š” ์ด ์ค‘๋ณต๋œ ๋ฌธ์ž๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜..
[BOJ] ๋“ฃ๋ณด์žก ์ฝ”๋“œ a,b = map(int,input().split()) arr = [input() for _ in range(a)] imp = [input() for _ in range(b)] arr = set(arr) imp = set(imp) result = sorted(list(arr&imp)) print(len(result)) for k in result: print(k) ๋ถ„์„ > ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€์ง€๋งŒ set ํ•จ์ˆ˜๋ฅผ ์“ฐ์ง€ ์•Š์œผ๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ setํ•จ์ˆ˜๋ผ๋ฆฌ ๊ฒน์น˜๋Š” ์›์†Œ๋งŒ ์ถ”์ถœํ•  ๋•Œ์—๋Š” sorted(list(a&b)) ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ ์œผ๋ฉด ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. 1764๋ฒˆ: ๋“ฃ๋ณด์žก ์ฒซ์งธ ์ค„์— ๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜ N, ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. ์ด์–ด์„œ ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„..
[Programmers] ๊ตฌ๋ช…๋ณดํŠธ ํ•ด์„ค ์ฝ”๋“œ def solution(people, limit): people.sort() i = 0 j = len(people) - 1 cnt = 0 while i
[Programmers] ์†Œ์ˆ˜ ์ฐพ๊ธฐ ํ•ด์„ค ์ฝ”๋“œ def solution(n): num = set(range(2,n+1)) for i in range(2,n+1): if i in num: num -= set(range(i*2,n+1,i)) return len(num) ๋ถ„์„ > set ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋จผ์ € ๋ฒ”์œ„ ๋‚ด์˜ ์ง‘ํ•ฉ์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ  i์˜ ๋ฐฐ์ˆ˜์ธ ์ง‘ํ•ฉ์„ ๋นผ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•˜์˜€๋‹ค. ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์†Œ์ˆ˜ ์ฐพ๊ธฐ 1๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž n ์‚ฌ์ด์— ์žˆ๋Š” ์†Œ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”. ์†Œ์ˆ˜๋Š” 1๊ณผ ์ž๊ธฐ ์ž์‹ ์œผ๋กœ๋งŒ ๋‚˜๋ˆ„์–ด์ง€๋Š” ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. (1์€ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.) ์ œํ•œ ์กฐ๊ฑด n์€ 2์ด์ƒ programmers.co.kr
[Programmers] ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ ์ฝ”๋“œ def keypad(current_N,next_N): dic = {'1':[0,0],'2':[0,1],'3':[0,2],'4':[1,0],'5':[1,1],'6':[1,2],'7':[2,0],'8':[2,1],'9':[2,2],'*':[3,0],'0':[3,1],'#':[3,2]} x1,y1 = dic[current_N] x2,y2 = dic[next_N] return abs(int(x1) - int(x2)) + abs(int(y1) - int(y2)) def solution(numbers,hand): ans = '' left = '*' right = '#' for i in numbers: if i in [1,4,7]: left = str(i) ans += 'L' elif i in [3,6,9]: ..
[Programmers] ์œ„์žฅ ์ฝ”๋“œ def solution(arr): dict = {} for i in range(len(arr)): if arr[i][1] not in dict: dict[arr[i][1]] = [arr[i][0]] else: dict[arr[i][1]].append(arr[i][0]) k = list(dict.values()) cnt = 1 for i in k: cnt *= len(i) + 1 return cnt - 1 ๋ถ„์„ > ํ•ด์‹œ๋งต์„ ๋งŒ๋“œ๋Š” ๊ฒƒ๊นŒ์ง€๋Š” ํ–ˆ๋Š”๋ฐ, ์กฐํ•ฉ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์—์„œ ๋ง‰ํ˜”๋˜ ๋ฌธ์ œ์˜€๋‹ค. ๊ทธ๋ž˜์„œ ๊ตฌ๊ธ€๋ง์œผ๋กœ ์•„์ด๋””์–ด๋ฅผ ์–ป์—ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด, ๋ฐฐ์—ด[[2,3,4],[1,0]] ์ด ์žˆ๋‹ค๋ฉด ์ฒซ๋ฒˆ์งธ ๋ฐฐ์—ด ๋‚ด์—์„œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ์กฐํ•ฉ์˜ ์ˆ˜๋Š” ์ด๋ ‡๊ฒŒ ..