-
Notifications
You must be signed in to change notification settings - Fork 43
/
DailyTemperatures.java
220 lines (191 loc) · 7.15 KB
/
DailyTemperatures.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package LeetCodeJava.Stack;
// https://leetcode.com/problems/daily-temperatures/
import java.util.*;
public class DailyTemperatures {
// V0
// IDEA : STACK (MONOTONIC STACK)
// LC 496
public int[] dailyTemperatures(int[] temperatures) {
if (temperatures.length == 1){
return temperatures;
}
/**
* Stack :
*
* -> cache elements (temperature) that DOESN'T have (NOT found) next warmer temperature yet
* -> structure : stack ([temperature, idx])
*/
Stack<List<Integer>> st = new Stack<>(); // element, idx
/** NOTE !!!
*
* can't use map, since there will be "duplicated" temperature
* -> which will cause different val has same key (hashMap key)
*/
//Map<Integer, Integer> map = new HashMap<>(); // {temperature : idx-of-next-warmer-temperature}
/**
* NOTE !!!
*
* we use nextGreater collect answer,
* -> idx : temperature, val : idx-of-next-warmer-temperature
*/
int[] nextGreater = new int[temperatures.length];
Arrays.fill(nextGreater, 0); // idx : temperature, val : idx-of-next-warmer-temperature
for (int j = 0; j < temperatures.length; j++){
int x = temperatures[j];
/**
* NOTE !!!
* 1) while loop
* 2) stack is NOT empty
* 3) cache temperature smaller than current temperature
*
* st.peek().get(0) is cached temperature
*/
while (!st.isEmpty() && st.peek().get(0) < x){
/**
* st.peek().get(1) is idx
*
*/
nextGreater[st.peek().get(1)] = j - st.peek().get(1);
st.pop();
}
List<Integer> cur = new ArrayList<>();
cur.add(x); // element
cur.add(j); // idx
st.add(cur);
}
//System.out.println("nextGreater = " + nextGreater);
return nextGreater;
}
// V0'
// IDEA : STACK (gpt)
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Stack/daily-temperatures.py#L34
public int[] dailyTemperatures_0_1(int[] temperatures) {
// Edge case: if the input array is null or empty, return an empty array
if (temperatures == null || temperatures.length == 0) {
return new int[0];
}
int n = temperatures.length;
int[] res = new int[n];
Stack<int[]> stack = new Stack<>();
// Loop through the temperatures in normal order
for (int i = 0; i < n; i++) {
/**
* While stack is not empty
* and the current temperature is greater
* than the temperature at the index stored at the top of the stack
*/
while (!stack.isEmpty() && stack.peek()[1] < temperatures[i]) {
/**
* Pop the top element from the stack
* and calculate the number of days to wait
*/
int[] top = stack.pop();
int idx = top[0];
res[idx] = i - idx;
}
/**
* NOTE !!!
*
* push 2 elements to stack
* 1) current idx
* 2) temperature
*
* e.g. : Push the current index and temperature onto the stack
*/
stack.push(new int[] { i, temperatures[i] });
}
return res;
}
// VO''
// IDEA : INCREASING STACK
// https://www.bilibili.com/list/525438321?sort_field=pubtime&spm_id_from=333.999.0.0&oid=779764003&bvid=BV1my4y1Z7jj
/** NOTE !!! WE USE "INCREASING" STACK HERE
*
* It's critical to define whether "increasing" or "decreasing" stack
* We're going to use in stack LC before implement it
*/
public int[] dailyTemperatures_0_2(int[] temperatures) {
if (temperatures.length == 0 || temperatures.equals(null)){
return new int[temperatures.length];
}
int[] res = new int[temperatures.length];
// TODO : double check this
Stack<int[]> stack = new Stack<>();
int[] init = new int[2];
init[0] = temperatures[0];
init[1] = 0;
stack.push(init);
for (int i = 1; i < temperatures.length; i++){
int cur_tmp = temperatures[i];
int[] _cur = new int[2];
/**
* data structure : [cur_temperature, index]
* so we save cur temperature as 1st element
* index of above element as 2nd element
* so we can compare temperature and get index difference via above
*/
_cur[0] = temperatures[i];
_cur[1] = i;
// case 1 : cur < stack top element
if (cur_tmp < stack.peek()[0]){
stack.push(_cur);
// case 2 : cur == stack top element
}else if (cur_tmp == stack.peek()[0]){
stack.push(_cur);
} // case 3 : cur > stack top element
else{
// make sure stack is NOT empty
while(!stack.empty() && stack.peek()[0] < cur_tmp){
int[] _top = stack.pop();
res[_top[1]] = i - _top[1];
}
int[] to_push = new int[2];
to_push[0] = cur_tmp;
to_push[1] = i;
stack.push(to_push);
}
}
return res;
}
// V1
// IDEA : Monotonic Stack
// https://leetcode.com/problems/daily-temperatures/editorial/
public int[] dailyTemperatures_2(int[] temperatures) {
int n = temperatures.length;
int[] answer = new int[n];
Deque<Integer> stack = new ArrayDeque<>();
for (int currDay = 0; currDay < n; currDay++) {
int currentTemp = temperatures[currDay];
// Pop until the current day's temperature is not
// warmer than the temperature at the top of the stack
while (!stack.isEmpty() && temperatures[stack.peek()] < currentTemp) {
int prevDay = stack.pop();
answer[prevDay] = currDay - prevDay;
}
stack.push(currDay);
}
return answer;
}
// V2
// IDEA : Array, Optimized Space
// https://leetcode.com/problems/daily-temperatures/editorial/
public int[] dailyTemperatures_3(int[] temperatures) {
int n = temperatures.length;
int hottest = 0;
int answer[] = new int[n];
for (int currDay = n - 1; currDay >= 0; currDay--) {
int currentTemp = temperatures[currDay];
if (currentTemp >= hottest) {
hottest = currentTemp;
continue;
}
int days = 1;
while (temperatures[currDay + days] <= currentTemp) {
// Use information from answer to search for the next warmer day
days += answer[currDay + days];
}
answer[currDay] = days;
}
return answer;
}
}