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 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); + }); +}); diff --git a/src/neetcode/stack/daily-temperatures/solution.ts b/src/neetcode/stack/daily-temperatures/solution.ts new file mode 100644 index 0000000..dbcdf85 --- /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 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 res; +};