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

์ „์ฒด ๊ธ€

(407)
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] x๋งŒํผ ๊ฐ„๊ฒฉ์ด ์žˆ๋Š” n๊ฐœ์˜ ์ˆซ์ž ์‰ฝ๋‹ค ใ…Žใ…Ž ๋‚ด ํ’€์ด def solution(x, n): a = [] for i in range(1,n+1): a.append(i*x) return a ๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ํ’€์ด def solution(x, n): return [i * x + x for i in range(n)] ํ•œ์ค„์ด๋ฉด ํ’€์ˆ˜์žˆ๋„ค.. ์‹ ๊ธฐํ•˜๋‹ค...ใ…Ž...
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ง์‚ฌ๊ฐํ˜• ๋ณ„์ฐ๊ธฐ ๋ณ„์ฐ๊ธฐ!! ๋„ˆ๋ฌด ์‰ฝ๋‹ค ใ…Ž a, b = map(int, input().strip().split(' ')) for i in range(b): for j in range(a): print("*",end="") print() ๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ a, b = map(int, input().strip().split(' ')) answer = ('*'*a +'\n')*b print(answer) ๋ณ„์ฐ๊ธฐ ์ด๋Ÿฐ์‹์œผ๋กœ ํ‘ธ๋Š”๊ฑด ์ฒ˜์Œ๋ณธ๋‹ค ์‹ ๊ธฐํ•˜๋‹ค ใ… ใ…  ๊ทผ๋ฐ ๋งˆ์ง€๋ง‰์— ๊ณต๋ฐฑ ํ•œ์ค„ ์ถ”๊ฐ€๋จ! ์ข‹์€ ์ฝ”๋“œ๋งž๋‚˜?..
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์œ„์žฅ ใ… ใ…  ๋”•์…”๋„ˆ๋ฆฌ๊นŒ์ง€ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ ๊ณ„์‚ฐ์„ ์–ด๋–ป๊ฒŒ ํ• ๊ฑด์ง€ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€.. ๋ธ”๋กœ๊ทธ๋ดค๋‹ค ใ…Ž.. ๋ธ”๋กœ๊ทธ์—์„œ ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•๋งŒ ๋ณด๊ณ  ์ฝ”๋“œ๋Š” ๋‚ด๊ฐ€ ์งœ๋ดค๋‹ค!ใ…  ์„ค๋ช…์€ ์ฝ”๋“œ ์•ˆ์— ๋“ค์–ด๊ฐ€์žˆ๋‹ค. ๋‚ด ์ฝ”๋“œ def solution(clothes): dic = {} sol=1 #์ข…๋ฅ˜๋ฅผ key, ์ˆ˜๋ฅผ value๋กœ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค. for key in clothes: if key[1] in dic: dic[key[1]]+=1 else: dic[key[1]] = 1 # ๊ฐ ์ข…๋ฅ˜๋ณ„๋กœ ์ž…๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณฑํ•ด์ค€๋‹ค. ๋งˆ์ง€๋ง‰์— ์ „๋ถ€ ์ž…์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋นผ์ค€๋‹ค. for key in dic: sol*=dic[key]+1 return sol-1 ๋‹ค๋ฅธ์‚ฌ๋žŒ ์ฝ”๋“œ def solution(clothes): from collections import Coun..
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ฐ€์šด๋ฐ ๊ธ€์ž ๊ฐ€์ ธ์˜ค๊ธฐ ๋‚ด๊ฐ€ ํ‘ผ ๊ฒƒ def solution(s): if len(s)%2==0: return s[len(s)//2-1:len(s)//2+1] else: return s[len(s)//2] ์ง์ˆ˜, ํ™€์ˆ˜ ๋‚˜๋ˆ„์–ด์„œ sliceํ•ด์คŒ ๋‹ค๋ฅธ์‚ฌ๋žŒ์ด ํ‘ผ ๊ฒƒ def string_middle(str): return str[(len(str)-1)//2:len(str)//2+1] ์ง์ˆ˜, ํ™€์ˆ˜๋ฅผ ๋‚˜๋ˆ„์ง€ ์•Š๋Š”๋‹ค. ex) ๋งŒ์•ฝ ๊ธธ์ด๊ฐ€ 5์ด๋ฉด str[2:3] ๊ธธ์ด๊ฐ€ 4๋ฉด str[1,3]
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] K๋ฒˆ์งธ์ˆ˜ ๋‚ด๊ฐ€ํ‘ผ๊ฒƒ def solution(array, commands): answer = [] for i in range(len(commands)): array_i = array[commands[i][0]-1:commands[i][1]] array_i.sort() answer.append(array_i[commands[i][2]-1]) return answer ๋จผ์ € array๋ฅผ ์ž˜๋ผ์„œ array_i์— ๋„ฃ์–ด์ค€๋‹ค. array_i๋ฅผ ์ •๋ ฌํ•œ๋’ค K๋ฒˆ์งธ ์ˆ˜๋ฅผ answer์— ๋„ฃ๋Š”๋‹ค. ๋‹ค๋ฅธ์‚ฌ๋žŒ์ด ํ‘ผ๊ฒƒ ์—ญ์‹œ.. ์ฝ”๋“œ๋“ค์ด ์งง๋‹ค.. def solution(array, commands): return list(map(lambda x:sorted(array[x[0]-1:x[1]])[x[2]-1], commands)) map์˜ ํ•จ์ˆ˜ ์นธ์—..
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค]ํ”„๋ฆฐํ„ฐ ์Šคํƒ, ํ๋ถ€๋ถ„ 2๋‹จ๊ณ„ ๋ฌธ์ œ ํ”„๋ฆฐํ„ฐ๋‹ค! ์œผ์œผ ใ… ใ…  ์–ด๋ ต๋‹ค.. ๋จผ์ € ๋‚ด๊ฐ€ ํ‘ผ ์ฝ”๋“œ์ด๋‹ค! def solution(priorities, location): cnt=0 while True: if priorities[0] < max(priorities): if location == 0: location = len(priorities) -1 else: location-=1 priorities.append(priorities.pop(0)) else: if location == 0: return cnt+1 else: cnt+=1 location-=1 priorities.pop(0) ๋จผ์ € priorities์˜ ์ฒซ๋ฒˆ์งธ ์›์†Œ์™€ priorities์—์„œ ์ œ์ผ ํฐ ์›์†Œ๋ฅผ ๋น„๊ตํ•œ๋‹ค. ๋งŒ์•ฝ priorities[0]๋ณด๋‹ค ํฐ ์›์†Œ๊ฐ€ ์žˆ์„..
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค]๊ธฐ๋Šฅ๊ฐœ๋ฐœ ์ฝ”๋“œ๋ฅผ ํšจ์œจ์ ์ด๊ฒŒ ์•ˆ์งœ์„œ ๊ทธ๋Ÿฐ์ง€ ๋ชจ๋ฅด๊ฒ ๋Š”๋ฐ.. ๋‹ต์€ ๋นจ๋ฆฌ๋‚˜์™”๋‹ค!! ์ผ๋‹จ ๋‹ค๋ฅธ ๋ถ„ ์ฝ”๋“œ ๋ณด๊ธฐ์ „์— ๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ ์„ค๋ช…์„ ์ ๊ฒ ๋‹ค.,, import math def solution(progresses, speeds): answer = [] for i in range(len(progresses)): progresses[i] = math.ceil((100-progresses[i])/speeds[i]) # ์ž‘์—…์ด ๋ฉฐ์น  ๊ฑธ๋ฆฌ๋Š”์ง€ ๊ณ„์‚ฐ์„ ํ•ด์ค€๋‹ค. cnt=0 while len(progresses)!=1: # progresses[1]๊ณผ ๋น„๊ตํ•˜๋ฏ€๋กœ ๊ธธ์ด๊ฐ€ 1์ด ์•„๋‹๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฌธ ์‹คํ–‰. if progresses[0] < progresses[1]: # ์ฒซ๋ฒˆ์งธ ์ž‘์—…์ด ๋‘๋ฒˆ์งธ ์ž‘์—…๋ณด๋‹ค ์ ๊ฒŒ ๊ฑธ๋ฆฌ๋ฉด progresses.pop(0) #..
ํŒŒ์ด์ฌ ์Šคํƒ, ํ ํŒŒ์ด์ฌ์—์„œ ์Šคํƒ๊ณผ ํ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…์‹œ๋‹ค!! ์Šคํƒ(Stack) - ๋‚˜์ค‘์— ๋„ฃ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋จผ์ € ๋ฐ˜ํ™˜๋˜๋„๋ก ์„ค๊ณ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ - Last In First Out (LIFO) - ๋ฐ์ดํ„ฐ์˜ ์ž…๋ ฅ์€ Push ( append() ), ์ถœ๋ ฅ์€ Pop ( pop() ) ํ(Queue) - ๋จผ์ € ๋„ฃ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋จผ์ € ๋ฐ˜ํ™˜๋˜๋„๋ก ์„ค๊ณ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ - First In First Out (FIFP) - ๋ฐ์ดํ„ฐ์˜ ์ž…๋ ฅ์€ Push ( append() ), ์ถœ๋ ฅ์€ Pop ( pop(0) ) ํ์—์„œ pop(0)์„ ํ•˜๋ฉด ์•ž์—์„œ ํ•˜๋‚˜๋ฅผ ๋น„์šฐ๊ณ  ๋’ค์— ์žˆ๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋“ค์„ ํ•œ์นธ์”ฉ ์•ž์œผ๋กœ ๋‹น๊ธฐ๋Š” ์ž‘์—…์„ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์‹œ์ž‘ ๋ณต์žก๋„๊ฐ€ O(n)์ด ๊ฑธ๋ฆฐ๋‹ค. (๋„ˆ๋ฌด ๋Š๋ฆผ!!) from collections import deque # (*d..