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 ith 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] Constraints: 1 <= temperatures.length <= 105 30 <= temperatures[i] <= 100
Solution In Java
import java.util.Stack;
public class DailyTemperatures {
public static int[] dailyTemperatures(int[] temperatures) {
int n = temperatures.length;
int[] result = new int[n];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
int index = stack.pop();
result[index] = i - index;
}
stack.push(i);
}
return result;
}
public static void main(String[] args) {
// Example 1
int[] temperatures1 = {73, 74, 75, 71, 69, 72, 76, 73};
int[] result1 = dailyTemperatures(temperatures1);
printResult(result1);
// Example 2
int[] temperatures2 = {30, 40, 50, 60};
int[] result2 = dailyTemperatures(temperatures2);
printResult(result2);
// Example 3
int[] temperatures3 = {30, 60, 90};
int[] result3 = dailyTemperatures(temperatures3);
printResult(result3);
}
private static void printResult(int[] result) {
for (int num : result) {
System.out.print(num + " ");
}
System.out.println();
}
}
This program defines a dailyTemperatures
method that takes an array of temperatures as input and returns an array of waiting days for each temperature. The Stack
is used to keep track of indices of temperatures for which we haven’t found a warmer day yet. The result array is populated based on the difference between the current index and the index popped from the stack when a warmer day is found. If no warmer day is found, the element in the result array is set to 0.
The main
method demonstrates the usage with the provided examples.
Solution In Python
def daily_temperatures(temperatures):
n = len(temperatures)
result = [0] * n
stack = []
for i in range(n):
while stack and temperatures[i] > temperatures[stack[-1]]:
index = stack.pop()
result[index] = i - index
stack.append(i)
return result
# Example 1
temperatures1 = [73, 74, 75, 71, 69, 72, 76, 73]
result1 = daily_temperatures(temperatures1)
print(result1)
# Example 2
temperatures2 = [30, 40, 50, 60]
result2 = daily_temperatures(temperatures2)
print(result2)
# Example 3
temperatures3 = [30, 60, 90]
result3 = daily_temperatures(temperatures3)
print(result3)
In this Python version, the logic is the same as the Java solution. The daily_temperatures
function takes an array of temperatures as input and returns a list of waiting days for each temperature. The stack
is used similarly to keep track of indices, and the result list is populated accordingly. The examples demonstrate the usage of the function with the provided test cases.