문제 Link
https://school.programmers.co.kr/learn/courses/30/lessons/42584 (opens in a new tab)
풀이
Brute Force
내가 원하던 풀이 방법이 이거였는데 이거마저도 못풀었다는게 너무 자괴감들고 괴롭다.
def solution(prices):
answer =[0] * len(prices)
for i in range(len(prices)):
for j in range(i+1, len(prices)):
if prices[i] <= prices[j]:
answer[i]+=1
else:
answer[i]+=1
break
return answer
Stack
from collections import deque
def solution(prices):
answer = []
prices = deque(prices)
while prices:
c = prices.popleft()
count = 0
for i in prices:
if c > i:
count += 1
break
count += 1
answer.append(count)
return answer
내 풀이(틀린거)
1
def solution(prices) :
pl = len(prices)
stack = []
ans = [0] * pl
out = 0
for i in range(pl) :
sl = len(stack)
tmp = 0
while sl != 0 and stack[sl - 1] > prices[i] :
stack.pop()
tmp += 1
ans[i - tmp - out] = tmp + out
sl = len(stack)
stack.append(prices[i])
out = tmp
for i in range(len(ans)) :
if ans[i] == 0 :
ans[i] = len(ans) - i - 1
return ans
2
def solution(prices) :
pl = len(prices)
ans = [0] * pl
stack = []
out = 0
bottom = 0
for cpi in range(pl) :
sl = len(stack)
while sl != 0 and stack[sl - 1] > prices[cpi] :
stack.pop()
out += 1
sl = len(stack)
if sl == 0 :
bottom = cpi
stack.append(prices[cpi])
sl = len(stack)
for x in range(sl) :
ans[bottom + x] = ans[bottom + x] + 1
return ans