-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path00739-daily_temperatures.java
More file actions
35 lines (26 loc) · 917 Bytes
/
00739-daily_temperatures.java
File metadata and controls
35 lines (26 loc) · 917 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
33
34
35
// 739: Daily Temperstures
// https://leetcode.com/problems/daily-temperatures/
import java.util.Stack;
class Solution {
// SOLUTION
public int[] dailyTemperatures(int[] temperatures) {
Stack<Integer> s = new Stack<>();
int[] result = new int[temperatures.length];
for (int i=0; i<temperatures.length; i++) {
while (!s.isEmpty() && temperatures[i]>temperatures[s.peek()]) {
result[s.peek()] = i - s.peek();
s.pop();
}
s.push(i);
}
return result;
}
public static void main(String[] args) {
Solution o = new Solution();
// INPUT
int[] temperatures = {73,74,75,71,69,72,76,73};
// OUTPUT
var result = o.dailyTemperatures(temperatures);
System.out.print("["); for (var v : result) System.out.print(v+" "); System.out.println("\b]");
}
}