Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.
Example 1:
Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
Example 2:
Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]
Example 3:
Input: temperatures = [30,60,90]
Output: [1,1,0]
Constraints:
- 1 <= temperatures.length <= 105
- 30 <= temperatures[i] <= 100
작성 코드
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
answer = []
for idx1, value in enumerate(temperatures):
isExist = 0
for idx2, com in enumerate(temperatures[idx1 + 1:]):
print(com, value)
if com > value :
isExist = 1
break
print(isExist)
answer.append(idx2 + isExist)
return answer
- stack 을 이용한 건 X
- 시간 복잡도가 O(N^2)
temperatures = [55,38,53,81,61,93,97,32,43,78]TestCase 에 대한 오류
이상 코드
from typing import List
class Solution:
def dailyTemperatures(self, T: List[int]) -> List[int]:
answer = [0] * len(T)
stack = []
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
- 처리되지 못한 인덱스 저장
반응형
'Algorithm > 파이썬 알고리즘 인터뷰' 카테고리의 다른 글
| [그리디 알고리즘] 주식을 사고팔기 가장 좋은 시점Ⅱ (0) | 2023.03.24 |
|---|---|
| [해시 테이블] 상위 K 빈도 요소 (1) | 2023.03.22 |
| [스택, 큐] 중복 문자 제거 (0) | 2023.03.13 |
| [연결 리스트] 두 정렬 리스트의 병합 (0) | 2023.03.09 |
| [트리] 정렬된 배열의 이진 탐색 트리 반환 (0) | 2022.05.16 |
댓글