Advertisement

Daily Temperatures - LeetCode 739 Solution

Daily Temperatures - Complete Solution Guide

Daily Temperatures is LeetCode problem 739, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 i th 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] C

Detailed Explanation

The problem asks us to find, for each day in a given list of daily temperatures, how many days we need to wait until we encounter a warmer temperature. If there is no warmer temperature in the future, we should record 0 for that day. The input is an array of integers representing daily temperatures, and the output is an array of the same size, where each element represents the number of days to wait for a warmer temperature.

Solution Approach

The solution uses a monotonic stack to efficiently find the next warmer temperature for each day. We iterate through the temperatures array, maintaining a stack of indices in decreasing temperature order. If we encounter a warmer temperature, we pop the indices from the stack until the stack is empty or the temperature at the top of the stack is greater than or equal to the current temperature. For each popped index, we calculate the number of days to wait and store it in the answer array. Finally, any remaining indices in the stack represent days for which no warmer temperature was found, and their corresponding values in the answer array remain 0.

Step-by-Step Algorithm

  1. Step 1: Initialize an array 'answer' of the same size as 'temperatures' with all elements set to 0. This will store the waiting days for each day.
  2. Step 2: Initialize an empty stack 'stack' to store indices of the temperatures.
  3. Step 3: Iterate through the 'temperatures' array using index 'i' and temperature 'temp'.
  4. Step 4: While the 'stack' is not empty and the current temperature 'temp' is greater than the temperature at the index on top of the 'stack':
  5. Step 5: Pop the index 'prev_index' from the 'stack'.
  6. Step 6: Calculate the waiting days as 'i - prev_index' and store it in 'answer[prev_index]'.
  7. Step 7: Push the current index 'i' onto the 'stack'.
  8. Step 8: After iterating through all the temperatures, return the 'answer' array.

Key Insights

  • Insight 1: Monotonic Stack is the ideal data structure. The stack will store indices of days in decreasing temperature order.
  • Insight 2: Iterating through the temperatures array, we can compare the current temperature with the temperature at the index at the top of the stack. If the current temperature is warmer, it means we have found the next warmer day for the temperature at the top of the stack.
  • Insight 3: The answer array can be pre-initialized with zeros, simplifying the case where no warmer temperature is found.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Stack, Monotonic Stack.

Companies

Asked at: Accenture, Agoda, Airwallex, Anduril, Flipkart, Grab, Huawei, Intuit, J.P. Morgan, Netflix, Okta, Palantir Technologies, PhonePe, SAP, Salesforce, ServiceNow, Swiggy, Visa, Walmart Labs, josh technology.