Skip to content

Commit a55f997

Browse files
authored
Update Daily Temperatures.java
1 parent 51add04 commit a55f997

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

Medium/Daily Temperatures.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
class Solution {
2-
public int[] dailyTemperatures(int[] T) {
3-
Stack<Integer> stack = new Stack<>();
4-
int[] ans = new int[T.length];
5-
for (int i = T.length - 1; i >= 0; i--) {
6-
while (!stack.isEmpty() && T[stack.peek()] <= T[i]) {
7-
stack.pop();
8-
}
9-
ans[i] = stack.isEmpty() ? 0 : (stack.peek() - i);
10-
stack.push(i);
2+
public int[] dailyTemperatures(int[] temperatures) {
3+
int n = temperatures.length;
4+
int[] result = new int[n];
5+
Stack<Integer> stack = new Stack<>();
6+
for (int i = n - 1; i >= 0; i--) {
7+
while (!stack.isEmpty() && temperatures[i] >= temperatures[stack.peek()]) {
8+
stack.pop();
9+
}
10+
result[i] = stack.isEmpty() ? 0 : stack.peek() - i;
11+
stack.push(i);
12+
}
13+
return result;
1114
}
12-
return ans;
13-
}
1415
}

0 commit comments

Comments
 (0)