Перейти к содержимому

LeetCode #739: Daily Temperatures, Monotonic Stack for the Next Warmer Day

Brute To Best

0:00 / 0:00

LeetCode #739: Daily Temperatures, Monotonic Stack for the Next Warmer Day

1 просмотр · 5 дн. назад
Brute To Best
102 подписчика
1 просмотр · 5 дн. назад
Brute to Best: every LeetCode problem, from the naive solution to the optimal one. In this video: The problem: for each day, find how many days you have to wait for a warmer temperature, or 0 if no warmer day comes. Brute force: from each day, scan to the right until you find a warmer day, which is O(N²). The key insight: keep a stack of the days that are still waiting, with their temperatures decreasing from bottom to top. When a warmer day arrives, pop each colder day off the stack and record the gap in days as its answer. Each index is pushed and popped at most once. Dry run: for 73, 74, 75, 71, 69, 72, 76, 73 the answer is 1, 1, 4, 2, 1, 1, 0, 0. The 75 waits 4 days for the 76. Complexity: O(N) time and O(N) space. Problem: https://leetcode.com/problems/daily-t... Related: Next Greater Element I (496), Online Stock Span (901), Asteroid Collision (735) Full Brute to Best playlist on the channel. Subscribe for a new LeetCode problem, solved from brute force to the best solution, every day. Hashtags #leetcode #leetcode739 #dailytemperatures #monotonicstack #stack #array #medium #coding #codinginterview #dsa #python #amazon #google #faang #brutetobest