Leetcode 739: Daily Temperatures
Given an array of integers temperatures representing the daily temperatures, return an array where each element is the number of days you need to wait after that day to get a warmer temperature. If there is no future day for which this is possible, keep the answer as 0.
Problem
Approach
Steps
Complexity
Input: The input is an array of integers representing the daily temperatures.
Example: temperatures = [72, 74, 78, 68, 65, 70, 80, 73]
Constraints:
• 1 <= temperatures.length <= 10^5
• 30 <= temperatures[i] <= 100
Output: Return an array where each element represents the number of days to wait for a warmer temperature.
Example: For temperatures = [72, 74, 78, 68, 65, 70, 80, 73], the output should be [1, 1, 4, 2, 1, 1, 0, 0].
Constraints:
Goal: Find the number of days to wait for a warmer temperature for each day in the input array.
Steps:
• Create an empty stack to store pairs of temperature and its index.
• Iterate over the temperatures from left to right.
• For each temperature, check if it is higher than the temperature at the index stored at the top of the stack.
• If so, pop the stack, calculate the difference in days, and store the result in the answer array.
• Push the current temperature and its index onto the stack.
• After processing all temperatures, fill in remaining stack indices with 0 (no warmer day).
Goal: The number of days should be between 1 and 10^5, and temperatures should range from 30 to 100.
Steps:
• 1 <= temperatures.length <= 10^5
• 30 <= temperatures[i] <= 100
Assumptions:
• The input array temperatures will always contain at least one element.
• Input: For temperatures = [72, 74, 78, 68, 65, 70, 80, 73], the output is [1, 1, 4, 2, 1, 1, 0, 0].
• Explanation: Day 1 has a higher temperature on day 2 (1 day later), day 2 has a higher temperature on day 3 (1 day later), and so on.
Approach: A stack-based approach to efficiently determine the number of days to wait for a warmer temperature.
Observations:
• We need to track the previous days' temperatures to know when we can find a warmer day.
• Using a stack to store temperatures and their indices will allow us to efficiently compute the answer for each day.
Steps:
• Initialize an empty stack and a result array with all zeros.
• Iterate through the temperatures list.
• For each day, check if the current temperature is higher than the one at the top of the stack.
• If so, pop the stack and update the result for the popped day with the number of days between them.
• Push the current temperature and its index onto the stack.
• Once the loop completes, any remaining days in the stack have no warmer day, so their result stays 0.
Empty Inputs:
• The input will always contain at least one temperature.
Large Inputs:
• The algorithm should efficiently handle inputs up to 100,000 elements.
Special Values:
• If the input contains strictly decreasing temperatures, all answers will be 0.
Constraints:
• The solution should handle both small and large inputs within the time limit.
vector<int> dailyTemperatures(vector<int>& temp) {
stack<pair<int,int>> stk;
vector<int> ans(temp.size(), 0);
for(int i = 0; i < temp.size(); i++) {
// cout << temp[i] << " " ;
while(!stk.empty() && temp[i] > stk.top().first) {
// cout << temp[i] << " " ;
pair<int, int> x = stk.top();
// cout << temp[i] << " " ;
stk.pop();
// cout << temp[i] << " " << x.first << x.second;
ans[x.second] = i-x.second;
// cout << temp[i] << " " ;
}
stk.push(make_pair(temp[i], i));
}
while(!stk.empty()) {
auto x = stk.top();
stk.pop();
ans[x.second] = 0;
}
return ans;
}
1 : Function Definition
vector<int> dailyTemperatures(vector<int>& temp) {
Defines the function that takes a vector of integers, `temp`, representing daily temperatures.
2 : Variable Declaration
stack<pair<int,int>> stk;
Declares a stack to store pairs of temperature and day index to track the previous higher temperatures.
3 : Variable Initialization
vector<int> ans(temp.size(), 0);
Initializes a result vector `ans` with zeros, each representing the number of days until a warmer temperature for each day.
4 : Loop Setup
for(int i = 0; i < temp.size(); i++) {
Starts a loop to iterate through each day in the input temperature vector.
5 : Loop Condition
while(!stk.empty() && temp[i] > stk.top().first) {
Starts a while loop to check if the current temperature is greater than the previous stored temperature in the stack.
6 : Variable Assignment
pair<int, int> x = stk.top();
Pops the top element from the stack and assigns it to `x`.
7 : Stack Operation
stk.pop();
Pops the top element from the stack after processing it.
8 : Result Update
ans[x.second] = i-x.second;
Updates the result array to record the number of days until a warmer temperature.
9 : Stack Push
stk.push(make_pair(temp[i], i));
Pushes the current temperature and its index onto the stack for future comparisons.
10 : While Loop Setup
while(!stk.empty()) {
Starts a while loop to handle any remaining elements in the stack after processing all temperatures.
11 : Variable Assignment
auto x = stk.top();
Pops the top element of the stack into variable `x` to process any remaining days that didn't get a warmer temperature.
12 : Stack Operation
stk.pop();
Pops the top element from the stack.
13 : Result Update
ans[x.second] = 0;
Sets the number of days to 0 for any temperatures that didn't have a warmer temperature.
14 : Return Statement
return ans;
Returns the result vector, which contains the number of days until a warmer temperature for each day.
Best Case: O(n), where n is the length of the temperatures array. This happens when the temperatures are strictly increasing.
Average Case: O(n), as we process each element in the temperatures array once.
Worst Case: O(n), where n is the length of the temperatures array. In the worst case, each element is processed once and added to the stack.
Description: The time complexity is O(n) because we only iterate through the array once, with each element being pushed and popped from the stack once.
Best Case: O(n), as the stack may store up to n elements in the worst case.
Worst Case: O(n), where n is the length of the temperatures array, due to the space required for the stack.
Description: The space complexity is O(n) because the stack can hold up to n elements in the worst case.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus