ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ฃผ์‹๊ฐ€๊ฒฉ

์ดˆ ๋‹จ์œ„๋กœ ๊ธฐ๋ก๋œ ์ฃผ์‹๊ฐ€๊ฒฉ์ด ๋‹ด๊ธด ๋ฐฐ์—ด prices๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€์ง€ ์•Š์€ ๊ธฐ๊ฐ„์€ ๋ช‡ ์ดˆ์ธ์ง€๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”. ์ œํ•œ์‚ฌํ•ญ prices์˜ ๊ฐ ๊ฐ€๊ฒฉ์€ 1 ์ด์ƒ 10,00

programmers.co.kr

 

๋ฌธ์ œ ํ’€์ด

 ์ดˆ ๋‹จ์œ„๋กœ ๊ธฐ๋ก๋œ ์ฃผ์‹ ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€์ง€ ์•Š๋Š” ๊ธฐ๊ฐ„์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ด๋ฅผ ์œ„ํ•ด prices์—์„œ ์ฒซ ๋ฒˆ์งธ ๊ฐ’๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ, ๋‹ค๋ฅธ ๊ฐ’๊ณผ ๋น„๊ตํ•˜์˜€์„ ๋•Œ ์–ด๋Š ์‹œ์ ์—์„œ ๊ฐ€๊ฒฉ์ด ํ•˜๋ฝํ•˜๋Š”์ง€๋ฅผ ํŒŒ์•…ํ•˜๋ฉด ๋œ๋‹ค. ๋ฌธ์ œ ํ’€์ด๋ฅผ ์œ„ํ•ด ํ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ๋„ ๋˜๊ณ , ๋‹จ์ˆœํžˆ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

  • ์ž…๋ ฅ๋˜๋Š” ์ฃผ์‹๊ฐ€๊ฒฉ์„ `deque`๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
  • ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ๊ฐ€์ ธ์™€, ์ฃผ์‹ ๊ฐ€๊ฒฉ๋“ค๊ณผ ๋น„๊ตํ•˜๋ฉฐ ์–ธ์ œ๊นŒ์ง€ ์ƒ์Šนํ•˜๋Š”์ง€ ์นด์šดํŠธํ•œ๋‹ค.
  • ์นด์šดํŠธ๋œ ๊ฐ’์„ `list`์— ๊ธฐ๋กํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

์ฝ”๋“œ

from collections import deque

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

    while prices:
        cnt = 0
        value = prices.popleft()
        for price in prices:
            cnt += 1
            if value > price:
                break
        answer.append(cnt)
    return answer
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€