Skip to content

Commit be873c7

Browse files
author
Hieu Luong
committed
Solve Problem 739 - Daily Temperature
1 parent cbed122 commit be873c7

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package monotone;
2+
3+
import java.util.Stack;
4+
5+
public class Problem739_DailyTemperature {
6+
7+
public int[] dailyTemperatures(int[] T) {
8+
// (73, 7), --> remove
9+
// (76, 6)
10+
// (72, 5) --> remove
11+
// (69, 4) --> remove
12+
// (71, 3) --> remove
13+
// (75, 2)
14+
15+
int n = T.length, res[] = new int[n];
16+
Stack<Integer> stack = new Stack<>();
17+
for (int i = n-1; i >= 0; i--) {
18+
while (!stack.isEmpty() && T[stack.peek()] <= T[i]) {
19+
stack.pop();
20+
}
21+
res[i] = stack.isEmpty() ? 0 : stack.peek()-i;
22+
stack.push(i);
23+
}
24+
return res;
25+
}
26+
}

0 commit comments

Comments
 (0)