JUST DO IT!

[TIL] KDT_20230414 ๋ณธ๋ฌธ

TIL

[TIL] KDT_20230414

sunhokimDev 2023. 4. 14. 16:03

๐Ÿ“š KDT WEEK 2 DAY 5 TIL

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ํ’€์ด์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•™์Šต

  • ํž™(Heap)
  • ๊นŠ์ด, ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(DFS / BFS)
  • ๋™์ ๊ณ„ํš๋ฒ•(Dynamic Programming)

 


 

ํž™(Heap) ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ์ •๋ ฌ(Heapsort)๊ณผ ์šฐ์„  ์ˆœ์œ„ ํ(priority queue)์— ์œ ์šฉ
  • ์ตœ๋Œ€, ์ตœ์†Œ ์›์†Œ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฐพ๋Š”๋ฐ ์œ ์šฉํ•˜๋‹ค!
  • ํŒŒ์ด์ฌ์— heapq ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ ๊ฐ€๋Šฅ

์˜ˆ์‹œ ์ฝ”๋“œ


import heapq # Heap ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

def solution(scoville, K):
    answer = 0
    heapq.heapify(scoville) # ํŠน์ • ๋ฆฌ์ŠคํŠธ๋ฅผ ํž™์œผ๋กœ ๋งŒ๋“ฆ
    
    while True: # ์•„๋ž˜ if ์กฐ๊ฑด์œผ๋กœ ๋ฐ˜๋ณต ์ข…๋ฃŒ
        min1 = heapq.heappop(scoville) # ํž™ pop
        
        if min1 >= K:
            break
        elif len(scoville) == 0: # ์•„๋ฌด๋ฆฌ ํ•ฉ์ณ๋„ K๋ฅผ ๋„˜์ง€๋Š” ๋ชปํ–ˆ์Œ
            return -1
        
        min2 = heapq.heappop(scoville)
        
        new = min1 + min2 * 2
        heapq.heappush(scoville, new) # ํž™ push
        answer += 1
    
    return answer

์Šค์ฝ”๋นŒ ์ง€์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์€ ์Œ์‹ ๋‘๊ฐœ๋ฅผ ํ•ฉ์ณ ๋ชจ๋“  ์Œ์‹์„ ํŠน์ • ์Šค์ฝ”๋นŒ ์ง€์ˆ˜(K)๋ฅผ ๋„˜๋„๋ก ๋งŒ๋“œ๋Š” ์ฝ”๋“œ.

์Šค์ฝ”๋นŒ์ง€์ˆ˜์˜ ๊ณ„์‚ฐ์€ new = min1 + min2 * 2 ์ด๋‹ค.

ํŒŒ์ด์ฌ์˜ heapq ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ์–ด ๊ตฌํ˜„์ด ๋งค์šฐ ํŽธ๋ฆฌํ•˜๋‹ค! ๐Ÿคญ


๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS)

  • ํ•œ ์ •์ ์—์„œ ์ธ์ ‘ํ•œ ๋ชจ๋“  ์ •์ ์„ ๋ฐฉ๋ฌธํ•˜๋˜, ๊ฐ ์ธ์ ‘ ์ •์ ์„ ๊ธฐ์ค€์œผ๋กœ ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์„ ๋๋‚ธ ํ›„ ๋ฐฉ๋ฌธ ์ง„ํ–‰
  • ์Šคํƒ์„ ์ด์šฉํ•˜์—ฌ ์–ด๋Š ์ •์ ์—์„œ DFS๋ฅผ ํ•˜๊ณ  ์žˆ๋Š”์ง€๋ฅผ ๊ธฐ์–ตํ•˜๊ณ  ๋˜๋Œ์•„๊ฐ€์•ผํ•จ
  • ์ •์ ์—์„œ ๊ธธ์ด ์žˆ์œผ๋ฉด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์„ ๋งŒํผ ๋“ค์–ด๊ฐ„(DFS) ๋‹ค์Œ, ๋ง‰ํ˜”์„ ๋•Œ ๊ธธ์ด ๋ณด์ผ ๋•Œ ๊นŒ์ง€ ๋˜๋Œ์•„๊ฐ€๋Š” ๊ฒƒ!

def solution(tickets):
    ticketDic = {}
    for t in tickets: # Dictionary์— tickets ์ €์žฅ
        ticketDic[t[0]] = ticketDic.get(t[0], []) + [t[1]]
        
    for tD in ticketDic: # ticket๋งˆ๋‹ค ๋„์ฐฉ์ง€๋ฅผ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌ
        ticketDic[tD].sort(reverse = True)
    
    stack = ["ICN"] #์ถœ๋ฐœ์ง€
    resultPath = [] #๊ฒฝ๋กœ
    while len(stack) > 0:
        top = stack[-1]
               
        if not top in ticketDic or len(ticketDic[top]) == 0: #๋‹ค์Œ ๊ฒฝ๋กœ ์ž์ฒด๊ฐ€ ์—†์—ˆ๊ฑฐ๋‚˜, ๋” ์ด์ƒ ํ‘œ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ
            resultPath.append(stack.pop()) 
        else:
            stack.append(ticketDic[top][-1])
            ticketDic[top] = ticketDic[top][:-1]
            
    return resultPath[::-1]

 tickets ์ธ์ž๋ฅผ ํ†ตํ•ด [์ถœ๋ฐœ์ง€, ๋„์ฐฉ์ง€] ํ‘œ ๋“ค์˜ ๋ฐฐ์—ด์„ ๋ฐ›์•„ ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ์ˆœํšŒํ•˜๋Š” path๋ฅผ ๋งŒ๋“œ๋Š” ๋ฌธ์ œ๋‹ค.

์Šคํƒ์œผ๋กœ ๊ตฌํ˜„ํ•œ DFS๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•œ๋‹ค.

์•ŒํŒŒ๋ฒณ ์ˆœ์œผ๋กœ ๋จผ์ € ์—ฌํ–‰ํ•ด์•ผ ํ•˜๋Š” ์กฐ๊ฑด์„ sort๋กœ ๋จผ์ € ํ•ด๊ฒฐํ–ˆ๊ณ ,

์—ฌํ–‰์„ ๋‹ค๋‹ˆ๋‹ค ๋‹ค์Œ ๊ฒฝ๋กœ๊ฐ€ ์—†๋Š” ์—ฌํ–‰์ง€๋ฅผ ๋งŒ๋‚˜๋ฉด, ๋จผ์ € path์— ๋„ฃ์–ด๋‘๊ณ  ๋งˆ์ง€๋ง‰์— ์—ญ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜์—ฌ ๋งˆ์ง€๋ง‰ ์—ฌํ–‰์ง€๋กœ ๋งŒ๋“œ๋Š” ๊ฒŒ ์ธ์ƒ์ ์ด์—ˆ๋‹ค. ๐Ÿ‘

DFS์˜ ๊ตฌ์กฐ์™€ ๋ฌธ์ œ์˜ ๊ด€๊ณ„๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐ์— ์˜ค๋ž˜ ๊ฑธ๋ ธ๋˜ ๋ฌธ์ œ..๐Ÿ˜‚


๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS)

  • ํ•œ ์ •์ ์—์„œ ์ธ์ ‘ํ•œ ๋ชจ๋“  ์ •์ ์„ ๋จผ์ € ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ๋ฌธํ•˜๊ณ , ๋ฐฉ๋ฌธํ•œ ์ธ์ ‘ ์ •์ ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐฉ๋ฌธํ•œ ์ˆœ์„œ์— ๋”ฐ๋ผ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•œ๋‹ค
  • ํ๋ฅผ ์ด์šฉํ•˜์—ฌ ์–ด๋Š ์ •์ ์—์„œ BFS๋ฅผ ํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๊ธฐ๋กํ•˜๊ณ  ์ง„ํ–‰ํ•ด์•ผ ํ•œ๋‹ค!

 


 

๋™์  ๊ณ„ํš๋ฒ•(Dynamic Programming)

  • ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ์ž‘์€ ๋ถ€๋ถ„ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆ„๊ณ , ํ•ด๋ฅผ ๊ตฌํ•œ ๋’ค ์žฌ๊ท€์ ์ธ ๋ฐฉ์‹์œผ๋กœ ์ „์ฒด ๋ฌธ์ œ์˜ ํ•ด๋‹ต์„ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ง„ํ–‰์— ๋”ฐ๋ผ ํƒ์ƒ‰ํ•ด์•ผ ํ•  ๋ฒ”์œ„๋ฅผ ๋™์ ์œผ๋กœ ๊ฒฐ์ •ํ•œ๋‹ค!
  • ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด, Knapsack Problem ๋“ฑ์ด ์ ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ์‹œ ์ฝ”๋“œ


def solution(N, number):
    s = [set() for x in range(8)] #set์œผ๋กœ ๋งŒ๋“ค์–ด ์ค‘๋ณต ์ œ๊ฑฐ ๊ฐ€๋Šฅ
    							  #์ฃผ์˜ : 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ง€๋งŒ, s[0]์—๋Š” N์„ ํ•œ๋ฒˆ ์‚ฌ์šฉํ•œ ์ง‘ํ•ฉ์ด ๋  ๊ฒƒ์ž„.
    
    for i, x in enumerate(s, start = 1): # ๋จผ์ € N, NN, NNN... ๊ณผ ๊ฐ™์€ ๋ฌธ์ž์—ด ๋„ฃ์–ด๋†“๊ธฐ
        x.add(int(str(N) * i))
        
    for i in range(len(s)): 
        for j in range(i):
            for op1 in s[j]:
                for op2 in s[i-j-1]:
                    s[i].add(op1 + op2)
                    s[i].add(op1 - op2)
                    s[i].add(op1 * op2)
                    if op2 != 0:
                        s[i].add(op1 // op2)
        if number in s[i]:
            answer = i + 1
            break
    
    else:
        answer = -1
        
    return answer

N์„ ์‚ฌ์น™์—ฐ์‚ฐ(๊ด„ํ˜ธ ํฌํ•จ)๋งŒ์„ ์‚ฌ์šฉํ•ด์„œ number๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ• ์ค‘ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•ด์•ผํ•œ๋‹ค.

ex) N = 5, number = 12 \\ 12 = (55 + 5) / 5 โžก answer = 4

๋ฌธ์ œ๋Š” ๊ฐ„๋‹จํ•˜์ง€๋งŒ, ๊ทธ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋งค์šฐ ๋งŽ์•„ ๋ณต์žกํ•œ ์—ฐ์‚ฐ์ด ํ•„์š”ํ–ˆ์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ ๋™์  ๊ณ„ํš๋ฒ•์„ ์ด์šฉํ•˜๋ฉด, ์ฝ”๋“œ๋กœ๋Š” ๋น„๊ต์  ๊ฐ„๋‹จํžˆ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค.(์—ฐ์‚ฐ๋„ ํ›จ์”ฌ ํšจ์œจ์ ์ด๋‹ค)

s[i]์— ์ „์— ํ–ˆ๋˜ ์—ฐ์‚ฐ๋“ค( i+1๊ฐœ์˜ N์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ˆซ์ž ์ง‘ํ•ฉ)์„ ์ €์žฅํ–ˆ๋‹ค๊ฐ€ ๋‹ค์Œ ์—ฐ์‚ฐ์—๋„ ํ™œ์šฉํ•จ์œผ๋กœ์จ ํšจ์œจ์ ์ธ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค!

 


 

๐Ÿค” ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์–ด๋ ค์› ๋˜ ๋‚ด์šฉ

์‚ฌ์†Œํ•˜๊ฒŒ ์ฐฉ๊ฐํ–ˆ๋˜ ๋ถ€๋ถ„์ด ๊ฑธ๋ฆผ๋Œ์ด ๋˜์–ด ์ดํ•ดํ•˜๋Š”๋ฐ ๋งŽ์€ ์‹œ๊ฐ„์„ ํ• ์• ํ–ˆ์—ˆ๋‹ค..๐Ÿ˜ฅ

๊ทธ๋ž˜๋„ ์ดํ•ดํ•˜๊ณ  ๋ณด๋‹ˆ ๋‚˜์ค‘์— ๊ผญ ํ•„์š”ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค์ด๋ผ๋Š” ๊ฒŒ ๋ชธ์œผ๋กœ ๋Š๊ปด์กŒ๋‹ค..

์—ด์‹ฌํžˆ ๋ฐฐ์› ์œผ๋‹ˆ ๋‚˜์ค‘์— ์•Œ์•„์„œ ํ™œ์šฉํ•  ์ค„ ์•Œ์•„์•ผ ํ• ํ…๋ฐ..๐Ÿ˜‡

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[TIL]KDT_20230418  (1) 2023.04.18
[TIL]KDT_20230417  (0) 2023.04.17
[TIL] KDT_20230413  (0) 2023.04.13
[TIL] KDT_20230412  (0) 2023.04.12
[TIL] KDT_20230411  (0) 2023.04.11