[Programmers / Python / 스택 큐] - 주식가격

Young·2021년 5월 24일
0

출처 https://programmers.co.kr/learn/courses/30/lessons/42584?language=python3

prices : 초 단위로 기록된 주식가격이 담긴 배열
가격이 떨어지지 않은 기간은 몇 초인지를 return 하는 solution 함수

효율성 테스트 통과 못 한 코드

stack 이용

def solution(prices):
    answer = []

    while prices:
        first = prices.pop(0)
        answer.append(0)
        
        for i in prices:
            answer[-1] += 1
            if first > i:
                break

    return answer
def solution(prices):
    answer = [0] * len(prices)
    prices_idx = 0

    while prices_idx < len(prices):

        for i in prices[prices_idx+1:]:
            answer[prices_idx] += 1

            if prices[prices_idx] > i:
                break
        prices_idx += 1

    return answer

2중 for문 이용

def solution(prices):
    answer = []
    
    for i in range(len(prices)):
    	answer.append(0)
       
        for j in prices[i+1:]:
            answer[-1] += 1
            if prices[i] > j :
                break
                
    return answer
def solution(prices):
    answer = [0]*len(prices)
    
    for num in range(len(prices)):
        increase = list(map(lambda x: (x - prices[num]) >= 0, prices[num+1:]))
        
        for i in increase:
            answer[num] += 1
            if not i:
                break
            
    return answer

효율성 테스트 통과한 코드

from collections import deque

def solution(prices):
    answer = []
    prices = deque(prices)
    
    while prices:
        first = prices.popleft()
        answer.append(0)

        for price in prices:
            answer[-1] += 1

            if first > price:
                break   
            
    return answer
profile
👩🏻‍💻

0개의 댓글

관련 채용 정보