-
Notifications
You must be signed in to change notification settings - Fork 21
/
739. Daily Temperatures.java
executable file
·84 lines (71 loc) · 3.07 KB
/
739. Daily Temperatures.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
M
tags: Stack, Hash Table, Monotonous Stack
time: O(n)
space: O(n)
#### Method1: Monotonous Stack
- Goal: given a index i, want right-side closest & higer number
- Draw example: right-most number at base, and builds up monotonous stack (mountain shape)
- add smaller item on top of stack
- keep popping if peek is higher than incoming
- space: O(n), time:O(n)
#### Method2: `Map <fixed value(temperature), Index>`, kinda of like bucket sort
- Refernece: https://leetcode.com/problems/daily-temperatures/solution/
- From right side:
- 1) record tempIndex[currTemp] = i;
- 2) Brutle find smallest temp index in range [currTemp + 1, 100] and record as result
```
/**
Given a list of daily temperatures T, return a list such that, for each day in the input,
tells you how many days you would have to wait until a warmer temperature.
If there is no future day for which this is possible, put 0 instead.
For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].
Note: The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].
*/
/*
Method1: Monotonous Stack
- Goal: given a index i, want right-side closest & higer number
- Draw example: right-most number at base, and builds up monotonous stack (mountain shape)
- add smaller item on top of stack
- keep popping if peek is higher than incoming
- space: O(n), time:O(n)
*/
class Solution {
public int[] dailyTemperatures(int[] T) {
int n = T.length;
int[] rst = new int[n];
Stack<Integer> stack = new Stack<>(); // note: store indexes
for (int i = n - 1; i >= 0; i--) {
int num = T[i];
// mono stack
while (!stack.isEmpty() && num >= T[stack.peek()]) stack.pop();
// process right-side closest & higer number
if (!stack.isEmpty() && num < T[stack.peek()]) rst[i] = stack.peek() - i;
else rst[i] = 0;
// add curr
stack.push(i);
}
return rst;
}
}
/*
#### Method2: Map <fixed value(temperature), Index>
- Refernece: https://leetcode.com/problems/daily-temperatures/solution/
- From right side: 1) record tempIndex[currTemp] = i; 2) Brutle find smallest temp index in range [currTemp + 1, 100] and record as result
*/
class Solution {
public int[] dailyTemperatures(int[] T) {
int n = T.length;
int[] rst = new int[n];
Integer[] tempIndex = new Integer[101]; // assume top temp is 101
for (int i = n - 1; i >= 0; i--) {
int num = T[i], warmerIndex = Integer.MAX_VALUE;;
for (int t = num + 1; t <= 100; t++) { // find the smallest index that has temp greater than num
if (tempIndex[t] != null && tempIndex[t] < warmerIndex) warmerIndex = tempIndex[t];
}
if (warmerIndex < Integer.MAX_VALUE) rst[i] = warmerIndex - i; // default rst[i] = 0
tempIndex[num] = i;
}
return rst;
}
}
```