-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution739.java
More file actions
32 lines (29 loc) · 894 Bytes
/
Copy pathsolution739.java
File metadata and controls
32 lines (29 loc) · 894 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
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Stack;
public class solution739 {
public int[] dailyTemperatures(int[] T) {
Stack<Integer> stack = new Stack<>();
int[] result = new int[T.length];
HashMap<Integer,Integer> hashMap = new LinkedHashMap<Integer, Integer>();
for(int i =0;i<T.length;i++)
{
result[i] = 0;
hashMap.put(i,T[i]);
}
for(int i = 0;i<T.length;i++){
if(stack.isEmpty()||T[i]<=hashMap.get(stack.peek()))
{
stack.push(i);
}else{
while(!stack.isEmpty()&&T[i]>hashMap.get(stack.peek()))
{
result[stack.peek()] = i-stack.peek();
stack.pop();
}
stack.push(i);
}
}
return result;
}
}