Skip to content

Commit 83598b9

Browse files
committed
leetcode practice
1 parent 0d9a900 commit 83598b9

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package kent.alg.leetcode;
2+
3+
import java.util.Stack;
4+
5+
import org.junit.Test;
6+
7+
public class DailyTemperatures {
8+
9+
public int[] dailyTemperatures(int[] T) {
10+
int[] ans = new int[T.length];
11+
Stack<Integer> stack = new Stack();
12+
13+
for (int i = T.length - 1; i >= 0; i--) {
14+
15+
while (!stack.isEmpty() && T[i] >= T[stack.peek()])
16+
stack.pop();
17+
18+
ans[i] = stack.isEmpty() ? 0 : stack.peek() - i;
19+
stack.push(i);
20+
}
21+
return ans;
22+
}
23+
24+
@Test
25+
public void test() {
26+
int[] T = {73, 74, 75, 71, 69, 72, 76, 73};
27+
System.out.println(dailyTemperatures(T));;
28+
}
29+
30+
}

0 commit comments

Comments
 (0)