From 6d3115fb661a7194bf06305ed597a06625636458 Mon Sep 17 00:00:00 2001 From: TheGeniusOfEternity Date: Sat, 1 Aug 2026 19:25:17 +0300 Subject: [PATCH 1/4] test(neetcode|roadmap|daily-temperatures): testcases --- .../stack/daily-temperatures/solution.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/neetcode/stack/daily-temperatures/solution.test.ts diff --git a/src/neetcode/stack/daily-temperatures/solution.test.ts b/src/neetcode/stack/daily-temperatures/solution.test.ts new file mode 100644 index 0000000..71f3916 --- /dev/null +++ b/src/neetcode/stack/daily-temperatures/solution.test.ts @@ -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); + }); +}); From ae1bf740f8008b139383dafd096e1de77fd04f0e Mon Sep 17 00:00:00 2001 From: TheGeniusOfEternity Date: Sat, 1 Aug 2026 19:25:36 +0300 Subject: [PATCH 2/4] feat(neetcode|roadmap|daily-temperatures): stack solution --- .../stack/daily-temperatures/solution.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/neetcode/stack/daily-temperatures/solution.ts diff --git a/src/neetcode/stack/daily-temperatures/solution.ts b/src/neetcode/stack/daily-temperatures/solution.ts new file mode 100644 index 0000000..97f4f42 --- /dev/null +++ b/src/neetcode/stack/daily-temperatures/solution.ts @@ -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 result = Array.from({ length: temperatures.length }, () => 0); + for (let i = temperatures.length - 1; i >= 0; i--) { + stack.push(temperatures[i]); + for (let j = 0; j < stack.length - 1; j++) { + if (stack[j] > temperatures[i]) { + result[i] = stack.length - j - 1; + } + } + } + return result; +}; From 665289d3fccbb215ff5b14a843d197da02a4ba45 Mon Sep 17 00:00:00 2001 From: TheGeniusOfEternity Date: Sat, 1 Aug 2026 19:25:58 +0300 Subject: [PATCH 3/4] feat(neetcode|roadmap|docs): update table of contents --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c28e673..67b929f 100644 --- a/README.md +++ b/README.md @@ -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)_ \ No newline at end of file + - [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)_ \ No newline at end of file From cd10fa710d7f71371d3556f34e784a8de5a8a4fd Mon Sep 17 00:00:00 2001 From: TheGeniusOfEternity Date: Sat, 1 Aug 2026 19:45:27 +0300 Subject: [PATCH 4/4] feat(neetcode|roadmap|daily-temperatures): more efficient solution --- .../stack/daily-temperatures/solution.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/neetcode/stack/daily-temperatures/solution.ts b/src/neetcode/stack/daily-temperatures/solution.ts index 97f4f42..dbcdf85 100644 --- a/src/neetcode/stack/daily-temperatures/solution.ts +++ b/src/neetcode/stack/daily-temperatures/solution.ts @@ -6,15 +6,15 @@ * will appear for the `ith` day, `result[i]` will be set to `0`. */ export const dailyTemperatures = (temperatures: number[]): number[] => { - const stack: number[] = []; - const result = Array.from({ length: temperatures.length }, () => 0); - for (let i = temperatures.length - 1; i >= 0; i--) { - stack.push(temperatures[i]); - for (let j = 0; j < stack.length - 1; j++) { - if (stack[j] > temperatures[i]) { - result[i] = stack.length - j - 1; - } + const stack: number[][] = []; + const res = new Array(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 result; + return res; };