๐ŸŒžAlgorithm/๐Ÿ”ฅprogrammers

[programmers] ์ด์ค‘์šฐ์„ ์ˆœ์œ„ํ

๋ฟŒ์•ผ._. 2021. 8. 25. 22:32

์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํž™ (Heap)


<์ด์ค‘์šฐ์„ ์ˆœ์œ„ํ>

๋ฌธ์ œ ์„ค๋ช…

 

์ด์ค‘ ์šฐ์„ ์ˆœ์œ„ ํ๋Š” ๋‹ค์Œ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.

๋ช…๋ น์–ด ์ˆ˜์‹  ํƒ‘(๋†’์ด)
I ์ˆซ์ž ํ์— ์ฃผ์–ด์ง„ ์ˆซ์ž๋ฅผ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.
D 1 ํ์—์„œ ์ตœ๋Œ“๊ฐ’์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
D -1 ํ์—์„œ ์ตœ์†Ÿ๊ฐ’์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

์ด์ค‘ ์šฐ์„ ์ˆœ์œ„ ํ๊ฐ€ ํ•  ์—ฐ์‚ฐ operations๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ,
๋ชจ๋“  ์—ฐ์‚ฐ์„ ์ฒ˜๋ฆฌํ•œ ํ›„ ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด [0,0] ๋น„์–ด์žˆ์ง€ ์•Š์œผ๋ฉด [์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’]์„ return ํ•˜๋„๋ก
solution ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์ฃผ์„ธ์š”.

 

 

์ œํ•œ ์‚ฌํ•ญ

 

- operations๋Š” ๊ธธ์ด๊ฐ€ 1 ์ด์ƒ 1,000,000 ์ดํ•˜์ธ ๋ฌธ์ž์—ด ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
- operations์˜ ์›์†Œ๋Š” ํ๊ฐ€ ์ˆ˜ํ–‰ํ•  ์—ฐ์‚ฐ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  : ์›์†Œ๋Š” “๋ช…๋ น์–ด ๋ฐ์ดํ„ฐ” ํ˜•์‹์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
    - ์ตœ๋Œ“๊ฐ’/์ตœ์†Ÿ๊ฐ’์„ ์‚ญ์ œํ•˜๋Š” ์—ฐ์‚ฐ์—์„œ ์ตœ๋Œ“๊ฐ’/์ตœ์†Ÿ๊ฐ’์ด ๋‘˜ ์ด์ƒ์ธ ๊ฒฝ์šฐ, ํ•˜๋‚˜๋งŒ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
- ๋นˆ ํ์— ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๋ผ๋Š” ์—ฐ์‚ฐ์ด ์ฃผ์–ด์งˆ ๊ฒฝ์šฐ, ํ•ด๋‹น ์—ฐ์‚ฐ์€ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค.

 

 

๋ฌธ์ œ ํ’€์ด

 

- my solution

import heapq

def solution(operations):
    result = []
    
    for i in operations:
        x = i.split()

        if x[0] == "I": # ์ˆซ์ž ์‚ฝ์ž…
            heapq.heappush(result, int(x[1]))
        elif x[0] == "D":
            if x[1] == "-1": # ์ตœ์†Ÿ๊ฐ’ ์‚ญ์ œ
                if len(result)!=0:
                    heapq.heappop(result)
            else: # ์ตœ๋Œ“๊ฐ’ ์‚ญ์ œ
                temp = []
                for j in result: # ์ตœ๋Œ€ ํž™ ๋งŒ๋“ค๊ธฐ
                    heapq.heappush(temp, -int(j))
                if len(temp) != 0: # ๋น„์–ด์žˆ์ง€ ์•Š์œผ๋ฉด ์‚ญ์ œ
                    heapq.heappop(temp)
                result = []
                for j in temp: # ์›๋ž˜๋Œ€๋กœ
                    heapq.heappush(result, -j)
        
    answer=[]
    if len(result) == 0: #ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด
        answer = [0, 0]
    else: # ๋น„์–ด์žˆ์ง€ ์•Š์œผ๋ฉด
        k = heapq.heappop(result) # ์ตœ์†Ÿ๊ฐ’
        temp = []
        for j in result: # ์ตœ๋Œ€ ํž™
            heapq.heappush(temp, -j)
        answer.append(-heapq.heappop(temp)) # ์ตœ๋Œ“๊ฐ’
        answer.append(k) # ์ตœ์†Ÿ๊ฐ’
            
    return answer

 

1) split์„ ํ†ตํ•˜์—ฌ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ช…๋ น์–ด๊ฐ€ I ์ผ ๋•Œ, D ์ผ ๋•Œ๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ๊ตฌํ˜„


์ƒ๊ฐ๐Ÿค”

 

Level 3์ด์—ˆ์ง€๋งŒ ํฌ๊ฒŒ ์–ด๋ ต์ง€ ์•Š์•˜๋˜ ๋ฌธ์ œ์˜€๋‹ค. ๋ ˆ๋ฒจ์ด ๋†’์•„ ๋ฌธ์ œ ํด๋ฆญํ•˜๊ธฐ ์–ด๋ ค์› ์„ ๋ฟ...

์ตœ๊ทผ์— heapq์— ๋Œ€ํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ดค๋˜ ๊ฒƒ์ด ํฌ๊ฒŒ ๋„์›€์ด ๋˜์—ˆ๋‹ค.

์œ„์—์„œ ์ฝ”๋“œ ์„ค๋ช…์ด ์งง์•˜๋˜ ๊ฒƒ๋„, heapq๋งŒ ์•Œ๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฑฐ๋ผ ์ƒ๊ฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

๋‚ด๊ฐ€ ์ƒ๊ฐํ•˜๊ธฐ์— ์ด ๋ฌธ์ œ์˜ ํฌ์ธํŠธ๋Š”

1) heapq ์‚ฌ์šฉ

2) ์ตœ์†Œ ํž™, ์ตœ๋Œ€ ํž™ 

์ธ ๊ฒƒ ๊ฐ™๋‹ค. ๋‹คํ–‰ํžˆ๋„ ์ตœ์†Œ ํž™, ์ตœ๋Œ€ ํž™์„ ๊ตฌํ˜„ํ•  ์ค„ ์•Œ๊ธฐ์— ๋นจ๋ฆฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ, ์‚ด์ง ์˜๋ฌธ์ ์ด ๋“œ๋Š” ๊ฒƒ์€, '์ตœ์†Œ ํž™์ด ๊ตฌํ˜„๋œ ์ƒํƒœ์—์„œ ์ตœ๋Œ€ ํž™์„ ๊ตฌํ˜„ํ•  ๋•Œ

๋ชจ๋“  ์›์†Œ์˜ ๊ฐ’์„ ๊ฐ€์ ธ์™€ ์ตœ๋Œ€ ํž™์œผ๋กœ ๋‹ค์‹œ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•๋ฐ–์— ์—†๋Š”๊ฐ€?'์˜€๋‹ค.

์กฐ๊ธˆ ๋” ๊ณต๋ถ€ํ•ด๋ด์•ผ๊ฒ ๋‹ค๐Ÿ˜ฅ

 

๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ์–ป์€ ๊ฒƒ

1. heapq

2. ์ตœ๋Œ€ ํž™, ์ตœ์†Œ ํž™

 


์ถœ์ฒ˜: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ์—ฐ์Šต, https://programmers.co.kr/learn/challenges