Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 그리디 알고리즘(Greedy Algorithm)
- DFS
- 백준 18352번
- DFS & BFS
- 이분 그래프(Bipartite Graph)
- 이분 탐색(Binary Search)
- 알고리즘 개념
- 동적 프로그래밍(Dynamic Programming)
- 큐(Queue)
- 다익스트라 알고리즘(Dijkstra Algorithm)
- 백준 2504번
- 스택(Stack)
- 백준 2812번
- 백준 17608번
- 트리(Tree)
- 백준 2261번
- 백준 1948번
- DFS(Depth First Search)
- 백준 21606번
- 분할 정복(Divide and Conquer)
- 위상 정렬(Topology Sort)
- 위상 정렬(Topological Sort)
- BFS
- 백준 1707번
- 백준 2493번
- BFS(Breadth First Search)
- 백준 9012번
- 그래프(Graph)
- 플로이드 워셜 알고리즘(Floyd-Warshall Algorithm)
- 백준 10000번
Archives
- Today
- Total
Always Be Wise
스택 : 일일 온도(리트코드 739번) 본문
728x90
▶ 문제 : https://leetcode.com/problems/daily-temperatures/
Daily Temperatures - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
##### 문제 #####
# 매일의 화시 온도 리스트를 입력 받아서, 더 따뜻한 날씨를 위해서는 며칠을 기다려야 하는지 출력하라.
##### 입력 #####
# temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
##### 출력 #####
# [1, 1, 4, 2, 1, 1, 0, 0]
▶ 접근 방법
입력 받은 온도 리스트에서 인덱스와 현재 온도를 반복문을 통해 구하고,
스택에 값이 있고, 스택의 마지막 값을 인덱스로 하는 온도 리스트의 값이 현재 온도보다 낮다면,
스택에서 값을 꺼내 해당 값을 answer 리스트의 인덱스로 하는 값을 갱신해준다.
만약 해당하지 않는다면 스택에 입력 받은 온도 리스트의 현재 인덱스를 추가해준다.
▶ 풀이 코드
def daily_temperatures(temperatures:list) -> list:
answer = [0] * len(temperatures)
stack = []
for i, cur in enumerate(temperatures):
while stack and cur > temperatures[stack[-1]]:
last = stack.pop()
answer[last] = i - last
stack.append(i)
return answer
temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
print(daily_temperatures(temperatures))
▶ 관련 링크
'알고리즘 > 리트코드' 카테고리의 다른 글
스택 : 중복 문자 제거(리트코드 316번) (0) | 2022.02.02 |
---|---|
스택 : 유효한 괄호(리트코드 20번) (0) | 2022.02.02 |
배열 : 주식을 사고 팔기 가장 좋은 시점(리트코드 121번) (0) | 2022.01.31 |
배열 : 세 수의 합(리트코드 15번) (0) | 2022.01.30 |
배열 : 빗물 트래핑(리트코드 42번) (0) | 2021.12.15 |
Comments