Skip to content

Commit db9fabc

Browse files
committed
stack-2: q & a
1 parent 1e1350b commit db9fabc

File tree

4 files changed

+137
-5
lines changed

4 files changed

+137
-5
lines changed

book/D-interview-questions-solutions.asc

+45-5
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ If the index overflows, it moves to the next node and reset the index to zero.
198198
[#stack-q-valid-parentheses]
199199
include::content/part02/stack.asc[tag=stack-q-valid-parentheses]
200200

201-
We need to validate that brackets are properly opened and closed, following these rules:
201+
.We need to validate that brackets are properly opened and closed, following these rules:
202202
- An opened bracket must be close by the same type.
203203
- Open brackets mush be closed in the corrent order.
204204

@@ -226,8 +226,48 @@ include::interview-questions/valid-parentheses.js[tag=solution]
226226
- Space: `O(n)`. We use an auxiliary stack.
227227

228228

229-
// [#linkedlist-q-TAG]
230-
// include::content/part02/linked-list.asc[tag=linkedlist-q-TAG]
229+
230+
[#stack-q-daily-temperatures]
231+
include::content/part02/stack.asc[tag=stack-q-daily-temperatures]
232+
233+
The first solution that might come to mind it's using two for loops. For each element we count how many elements ahead there's a warmer temperature.
234+
235+
[source, javascript]
236+
----
237+
include::interview-questions/daily-temperatures.js[tag=dailyTemperaturesBrute1]
238+
----
239+
240+
This solution is a `O(n^2)`. Can we do better? We can!
241+
242+
Here's an idea: start backwards so we know when there's a warmer temperature before hand. The last element is always 0 (because there's no more temperatures ahead of it). We can place each element's index that we visit on a stack. If the current temperature is bigger than the on the top of the stack, we remove it until a bigger one remains or the stack is empty. If the stack has a value, we calculate the number of days ahead.
243+
244+
*Algorithm*:
245+
246+
- Traverse the daily temperatures backwards
247+
- Push each temperature to an stack.
248+
- While the current temperature is larger than the one at the top of the stack, remove it.
249+
- If the stack is empty, then there's no warmer weather ahead.
250+
- If the stack has an element, calculate the index delta.
251+
252+
*Implementation*:
253+
254+
[source, javascript]
255+
----
256+
include::interview-questions/daily-temperatures.js[tag=description]
257+
include::interview-questions/daily-temperatures.js[tag=solution]
258+
----
259+
260+
The stack contains the indexes rather than the temperatures.
261+
262+
*Complexity Analysis*:
263+
264+
- Time: `O(n)`. We visit each element on the array once.
265+
- Space: `O(1)`. The worst case scenario is ascending order without duplicates. The stack will hold at most 70 items (100 - 30). If we didn't have the range restriction then space complexity would be `O(n)`.
266+
267+
268+
269+
// [#linkedlist-q-FILENAME]
270+
// include::content/part02/linked-list.asc[tag=linkedlist-q-FILENAME]
231271

232272
// RESTATE REQUIREMENTS AND DESCRIPTIONS
233273

@@ -242,8 +282,8 @@ include::interview-questions/valid-parentheses.js[tag=solution]
242282

243283
// [source, javascript]
244284
// ----
245-
// include::interview-questions/TAG.js[tag=description]
246-
// include::interview-questions/TAG.js[tag=solution]
285+
// include::interview-questions/FILENAME.js[tag=description]
286+
// include::interview-questions/FILENAME.js[tag=solution]
247287
// ----
248288

249289
// IMPLEMENTATION NOTES

book/content/part02/stack.asc

+23
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ It's not very common to search for values on a stack (other Data Structures are
9090
==== Interview Questions
9191
(((Interview Questions, Arrays)))
9292

93+
94+
95+
96+
9397
// tag::stack-q-valid-parentheses[]
9498
===== Validate Parentheses / Braces / Brackets
9599

@@ -104,3 +108,22 @@ include::../../interview-questions/valid-parentheses.js[tag=description]
104108
----
105109

106110
_Solution: <<stack-q-valid-parentheses>>_
111+
112+
113+
114+
115+
116+
// tag::stack-q-daily-temperatures[]
117+
===== Daily Temperaturs
118+
119+
*ST-2*) _Given an array of integers from 30 to 100 (daily temperatures), return another array that for each day in the input, tells you how many days you would have to wait until a warmer temperature. If no warmer temparature is possible then return `0` for that element._
120+
// end::stack-q-daily-temperatures[]
121+
122+
[source, javascript]
123+
----
124+
include::../../interview-questions/daily-temperatures.js[tag=description]
125+
// write you code here
126+
}
127+
----
128+
129+
_Solution: <<stack-q-daily-temperatures>>_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// tag::description[]
2+
/**
3+
* Given an array with daily temperatures (30 °C to 100 °C),
4+
* return an array with the days count until a warmer temperature
5+
* for each elem from the input.
6+
*
7+
* @examples
8+
* dailyTemperatures([30, 28, 50, 40, 30]); // [2, 1, 0, 0, 0]
9+
* dailyTemperatures([73, 69, 72, 76, 73]); // [3, 1, 1, 0, 0]
10+
*
11+
* @param {number[]} t - Daily temperatures
12+
*/
13+
function dailyTemperatures(t) {
14+
// end::description[]
15+
// tag::solution[]
16+
const last = (arr) => arr[arr.length - 1];
17+
const stack = [];
18+
const ans = [];
19+
20+
for (let i = t.length - 1; i >= 0; i--) {
21+
while (stack.length && t[i] >= t[last(stack)]) stack.pop();
22+
ans[i] = stack.length ? last(stack) - i : 0;
23+
stack.push(i);
24+
}
25+
26+
return ans;
27+
}
28+
// end::solution[]
29+
30+
// tag::dailyTemperaturesBrute1[]
31+
function dailyTemperaturesBrute1(t) {
32+
const ans = [];
33+
34+
for (let i = 0; i < t.length; i++) {
35+
ans[i] = 0;
36+
for (let j = i + 1; j < t.length; j++) {
37+
if (t[j] > t[i]) {
38+
ans[i] = j - i;
39+
break;
40+
}
41+
}
42+
}
43+
44+
return ans;
45+
}
46+
// end::dailyTemperaturesBrute1[]
47+
48+
module.exports = { dailyTemperatures, dailyTemperaturesBrute1 };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const { dailyTemperatures } = require('./daily-temperatures');
2+
3+
describe('Stack: Daily Temperatures', () => {
4+
it('should work', () => {
5+
expect(dailyTemperatures([30, 28, 50, 40, 30])).toEqual([2, 1, 0, 0, 0]);
6+
});
7+
8+
it('should work', () => {
9+
expect(dailyTemperatures([73, 74, 75, 71, 69, 72, 76, 73])).toEqual([1, 1, 4, 2, 1, 1, 0, 0]);
10+
});
11+
12+
it('should work', () => {
13+
expect(dailyTemperatures([89, 62, 70, 58, 47, 47, 46, 76, 100, 70])).toEqual([8, 1, 5, 4, 3, 2, 1, 1, 0, 0]);
14+
});
15+
16+
it('should work with large data', () => {
17+
const input = [64, 40, 49, 73, 72, 35, 68, 83, 35, 73, 84, 88, 96, 43, 74, 63, 41, 95, 48, 46, 89, 72, 34, 85, 72, 59, 87, 49, 30, 32, 47, 34, 74, 58, 31, 75, 73, 88, 64, 92, 83, 64, 100, 99, 81, 41, 48, 83, 96, 92, 82, 32, 35, 68, 68, 92, 73, 92, 52, 33, 44, 38, 47, 88, 71, 50, 57, 95, 33, 65, 94, 44, 47, 79, 41, 74, 50, 67, 97, 31, 68, 50, 37, 70, 77, 55, 48, 30, 77, 100, 31, 100, 69, 60, 47, 95, 68, 47, 33, 64];
18+
const output = [3, 1, 1, 4, 3, 1, 1, 3, 1, 1, 1, 1, 30, 1, 3, 2, 1, 25, 2, 1, 19, 2, 1, 3, 2, 1, 11, 5, 1, 1, 2, 1, 3, 2, 1, 2, 1, 2, 1, 3, 2, 1, 0, 46, 3, 1, 1, 1, 30, 18, 5, 1, 1, 2, 1, 12, 1, 10, 5, 1, 2, 1, 1, 4, 3, 1, 1, 11, 1, 1, 8, 1, 1, 5, 1, 3, 1, 1, 11, 1, 3, 2, 1, 1, 5, 3, 2, 1, 1, 0, 1, 0, 3, 2, 1, 0, 0, 2, 1, 0];
19+
expect(dailyTemperatures(input)).toEqual(output);
20+
});
21+
});

0 commit comments

Comments
 (0)