
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
L = len(temperatures)
temper, stack = [0] * L, []
for i in range(L):
t = temperatures[i]
while stack and t > temperatures[stack[-1]]:
idx = stack.pop()
temper[idx] = i - idx
stack.append(i)
return temper
로직은 동일한데 변수명이 내포하고 있는 의미들이 불명확한 것 같아서 교재를 참고해 개선
class Solution:
def dailyTemperatures(self, T: List[int]) -> List[int]:
L = len(T)
answer, stack = [0] * L, []
for i, cur in enumerate(T):
while stack and cur > T[stack[-1]]:
last = stack.pop() # 스택에 쌓여있던 이전 인덱스
answer[last] = i - last # 현재 위치 - 이전 위치(거리)
stack.append(i)
return answer
프로그래머스 주식 가격과 유사