Skip to content

Latest commit

 

History

History

739-DailyTemperatures

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Daily Temperatures

Problem can be found in here!

Solution: Monotonic Stack

def dailyTemperatures(temperatures: List[int]) -> List[int]:
    n = len(temperatures)
    days_to_wait = [0] * n
    stack = []

    for i in range(n):
        while stack and temperatures[stack[-1]] < temperatures[i]:
            index = stack.pop()
            days_to_wait[index] = i-index

        stack.append(i)

    return days_to_wait

Time Complexity: O(n), Space Complexity: O(n)