Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@
- [Task #15](src/neetcode/sliding-window/best-time-to-buy-and-sell-stock) – _Best Time To Buy And Sell Stock_ - _[check the task](https://neetcode.io/problems/buy-and-sell-crypto)_
- [Task #16](src/neetcode/sliding-window/longest-substring-without-repeating-characters) – _Longest Substring Without Repeating Characters_ - _[check the task](https://neetcode.io/problems/longest-substring-without-duplicates)_
- [Task #17](src/neetcode/sliding-window/longest-repeating-character-replacement) – _Longest Repeating Character Replacement_ - _[check the task](https://neetcode.io/problems/longest-repeating-substring-with-replacement)_
- [Task #17](src/neetcode/sliding-window/longest-repeating-character-replacement) – _Longest Repeating Character Replacement_ - _[check the task](https://neetcode.io/problems/longest-repeating-substring-with-replacement)_
- [Task #18](src/neetcode/sliding-window/permutation-in-string) – _Permutation In String_ - _[check the task](https://neetcode.io/problems/permutation-string)_
- [Task #19](src/neetcode/sliding-window/minimum-window-substring) – _Minimum Window Substring_ - _[check the task](https://neetcode.io/problems/minimum-window-with-characters)_
- [Task #20](src/neetcode/sliding-window/sliding-window-maximum) – _Sliding Window Maximum_ - _[check the task](https://neetcode.io/problems/sliding-window-maximum)_
- Stack
- [Task #21](src/neetcode/stack/valid-parentheses) – _Valid Parentheses_ - _[check the task](https://leetcode.com/problems/valid-parentheses)_
- [Task #22](src/neetcode/stack/min-stack) – _Min Stack_ - _[check the task](https://neetcode.io/problems/minimum-stack)_
- [Task #23](src/neetcode/stack/evaluate-reverse-polish-notation) – _Evaluate Reverse Polish Notation_ - _[check the task](https://neetcode.io/problems/evaluate-reverse-polish-notation)_
- [Task #23](src/neetcode/stack/evaluate-reverse-polish-notation) – _Evaluate Reverse Polish Notation_ - _[check the task](https://neetcode.io/problems/evaluate-reverse-polish-notation)_
- [Task #24](src/neetcode/stack/daily-temperatures) – _Daily Temperatures_ - _[check the task](https://neetcode.io/problems/daily-temperatures)_
17 changes: 17 additions & 0 deletions src/neetcode/stack/daily-temperatures/solution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { dailyTemperatures } from './solution';

describe('Daily Temperatures | NeetCode | RoadMap | Testcases', () => {
test('#1 Average case', () => {
const temperatures = [30, 38, 30, 36, 35, 40, 28];
const expected = [1, 4, 1, 2, 1, 0, 0];
const output = dailyTemperatures(temperatures);
expect(output).toEqual(expected);
});

test('#2 No warmer temperatures', () => {
const temperatures = [22, 21, 20];
const expected = [0, 0, 0];
const output = dailyTemperatures(temperatures);
expect(output).toEqual(expected);
});
});
20 changes: 20 additions & 0 deletions src/neetcode/stack/daily-temperatures/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {number[]} temperatures - array of integers where `temperatures[i]` represents
* the daily temperatures on the `ith` day.
* @return {number[]} - where `result[i]` is the number of days after the `ith` day before a warmer
* temperature appears on a future day. If there is no day in the future where a warmer temperature
* will appear for the `ith` day, `result[i]` will be set to `0`.
*/
export const dailyTemperatures = (temperatures: number[]): number[] => {
const stack: number[][] = [];
const res = new Array<number>(temperatures.length).fill(0);
for (let i = 0; i < temperatures.length; i++) {
const t = temperatures[i];
while (stack.length > 0 && t > stack[stack.length - 1][0]) {
const [, stackInd] = stack.pop() ?? [-1, -1];
res[stackInd] = i - stackInd;
}
stack.push([t, i]);
}
return res;
};
Loading