๐Ÿ—๏ธ Algorithm/โฌ› ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

โฌ› [Programmers] [Python] [Level2] ์ฃผ์‹๊ฐ€๊ฒฉ

Dbswnstjd 2023. 4. 10. 01:13

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/42584

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

ํ’€์ด

# ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2๋‹จ๊ณ„ - ์ฃผ์‹ ๊ฐ€๊ฒฉ
from collections import deque

def solution(prices):
    queue = deque(prices)
    answer = []

    while queue:
        price = queue.popleft()
        sec = 0
        for q in queue:
            sec += 1
            if price > q:
                break
        answer.append(sec)
    return answer

๋ฐํฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค. 

q๊ฐ€ ๋นŒ๋•Œ ๊นŒ์ง€ while๋ฌธ์„ ๋Œ๊ณ  q์—์„œ ํ•˜๋‚˜ ์”ฉ ๋นผ์„œ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•œ๋‹ค. 

์ฒ˜์Œ์— ์ดํ•ด๋ฅผ ์ž˜๋ชปํ•œ ๊ฒƒ์ด ๊ธฐ์ค€์„ ์ž˜๋ชป ์žก์•˜๋‹ค.
1์ดˆ์—์„œ 1์›์ผ๋•Œ 4์ดˆ๋’ค์— ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง„๋‹ค๋Š” ๊ฒƒ์„ 3์ดˆ์—์„œ 3์›์—์„œ 2์›์œผ๋กœ ๋˜๋Š” ๋•Œ ๋–จ์–ด์ง€๋Š” ๊ฒƒ์œผ๋กœ ์ž˜ ๋ชป ์ดํ•ดํ–ˆ๋‹ค. ๋ฌธ์ œ๋ฅผ ์ œ๋Œ€๋กœ ์ฝ์–ด๋ณด๊ณ  ์ดํ•ดํ•˜๋Š” ๋Šฅ๋ ฅ์„ ๋” ๊ธธ๋Ÿฌ์•ผ๊ฒ ๋‹ค.