-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDaily Temperatures.java
More file actions
32 lines (29 loc) · 855 Bytes
/
Daily Temperatures.java
File metadata and controls
32 lines (29 loc) · 855 Bytes
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
29
30
31
32
class Solution {
public int[] dailyTemperatures(int[] temp) {
// int[] arr = new int[temperatures.length];
// for(int i=0; i<temperatures.length; i++){
// arr[i] = 0;
// for(int j=i; j<temperatures.length; j++){
// if(temperatures[j] > temperatures[i]){
// arr[i] = j-i;
// break;
// }
// }
// }
// return arr;
//TLE
int n = temp.length;
int res[] = new int[n];
Stack<Integer> st = new Stack<>();
for(int i = 0; i < n; i++)
{
while(!st.isEmpty() && temp[i] > temp[st.peek()])
{
res[st.peek()] = i - st.peek();
st.pop();
}
st.push(i);
}
return res;
}
}