-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDaily Temperatures.java
More file actions
28 lines (23 loc) · 1.15 KB
/
Daily Temperatures.java
File metadata and controls
28 lines (23 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
//result array
int[] result = new int[temperatures.length];
//if length == 0, return the result
if(temperatures.length == 0)
return result;
//monotonic stack(decreasing)
Stack<Integer> mono_stack = new Stack<>();
//iterate throguh the array and build a decreasing monotonic stack, but if greater element is found compared to the top of the stack, calculate its distance from the larger number and store the distance in the result array, and pop the number from stack, do this until stack is empty or lesser element is encountered, and just keep pushing the number on the stack if its lesser(decreasing stack)
for(int i=0; i < temperatures.length; i++)
{
while(!mono_stack.empty() && temperatures[mono_stack.peek()] < temperatures[i])
{
result[mono_stack.peek()] = i - mono_stack.peek();
mono_stack.pop();
}
mono_stack.push(i);
}
//return the result
return result;
}
}