[큐/스택] 주식가격 (Level 2)

정은경·2020년 4월 4일
0

1. 문제

2. 나의 풀이

2-1) 시간초과나는 나의 풀이

import copy

def solution(prices):
    rlt = []
    temp = prices

    while prices:
        target = prices.pop(0)
        count = 0
        temp = copy.deepcopy(prices)
        # print(temp, prices)

        count = 0
        while prices:
            count +=1
            t = prices.pop(0)
            if t < target:
                break;

        # print("haha",temp)
        prices = temp
        rlt.append(count)

    rlt[-1] = 0
    return rlt

2-2) 남의 풀이를 참고한 풀이

  • 신박하다!
def solution(prices):
    answer = [0] * len(prices)
    
    for i in range(len(prices)-1):
        for j in range(i, len(prices)-1):
            if prices[i] > prices[j]:
                break
            else:
                answer[i] += 1
    return answer

3. 남의 풀이

def solution(prices):
  answer = []

  for i in range(0, len(prices)-1):
    fin = False
    for j in range(i+1, len(prices)):
      if prices[i] > prices[j]:
        answer.append(j-i)
        fin = True
        break
    if fin == False:
      answer.append(len(prices)-i-1)

  answer.append(0)

  return answer

Reference

느낀 점

profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글