프로그래밍/Python

프로그래머스_주식가격

donie 2020. 9. 28. 12:12

1. 문제

출처 : 프로그래머스

2. 소스코드 (python3)

def solution(prices):
    answer = []
    
    for i in range(len(prices)-1): # i = 0 ~ 마지막-1
        cnt = 1
        for j in range(i+1, len(prices)-1): #j = i+1 ~ 마지막
            if prices[i] <= prices[j]: cnt += 1
            else : break
        answer.append(cnt)
        #print(i, answer)
    answer.append(0)

    return answer

 

3. 고찰

1) 시간복잡도

for문을 i : 0 ~ N-1 , j : i+1 ~ N 반복한다.

append함수의 시간복잡도는 O(1)

∴ O(N^2)

 

2) list가 비었는지 확인 

stack = []

if stack : print("stack is not empty")

if not stack : print("stack is empty")

 

3) 후위연산자

python에는 후위연산자가 존재하지 않는다.

 

4) list의 길이 구하는 방법

len(list)