devHudi / hudi.blog

개인 블로그
https://hudi.blog
MIT License
18 stars 3 forks source link

programmers-42584/ #2

Closed utterances-bot closed 2 weeks ago

utterances-bot commented 3 years ago

[PS #09] 주식가격 (42584)

https://hudi.blog/programmers-42584/

changwoomon commented 3 years ago

위쪽 코드에서 list -> deque로만 바꿔줘도 효율성 통과됩니다!

from collections import deque

def solution(prices):
    ret = []
    q = deque(prices)

    while len(q) > 0:
        cur_price = q.popleft()

        s = 0
        for p in q:
            s += 1
            if cur_price > p:
                break
        ret.append(s)

    return ret
devHudi commented 3 years ago

@changwoomon

오... 찾아보니 deque 는 내부적으로 이중 링크드 리스트로 구현되어있다고 하네요. 이로 인해서 popleft 같은 메소드도 O(1) 의 시간복잡도로 빠르게 작동하나봅니다.

좋은 인사이트 얻고가요 감사합니다!