Skip to content

Commit

Permalink
255th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Dec 23, 2024
1 parent 6c5fe12 commit 542cb4c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,12 @@ Ace Coding Interview with 75 Qs
[435]: ./src/page-5/435.%20Non-overlapping%20Intervals/eraseOverlapIntervals.ts
[452]: ./src/page-5/452.%20Minimum%20Number%20of%20Arrows%20to%20Burst%20Balloons/findMinArrowShots.ts

| Monotonic Stack | | |
| ----------------------- | -------- | ------ |
| 739. Daily Temperatures | Solution | Medium |
| 901. Online Stock Span | Solution | Medium |
| Monotonic Stack | | |
| ----------------------- | --------------- | ------ |
| 739. Daily Temperatures | [Solution][739] | Medium |
| 901. Online Stock Span | Solution | Medium |

[739]: ./src/page-7/739.%20Daily%20Temperatures/dailyTemperatures.ts

</details>

Expand Down
23 changes: 23 additions & 0 deletions src/page-7/739. Daily Temperatures/dailyTemperatures.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { dailyTemperatures } from './dailyTemperatures';

describe('739. Daily Temperatures', () => {
test('dailyTemperatures', () => {
{
const temperatures = [73, 74, 75, 71, 69, 72, 76, 73];
const expected = [1, 1, 4, 2, 1, 1, 0, 0];
expect(dailyTemperatures(temperatures)).toStrictEqual(expected);
}

{
const temperatures = [30, 40, 50, 60];
const expected = [1, 1, 1, 0];
expect(dailyTemperatures(temperatures)).toStrictEqual(expected);
}

{
const temperatures = [30, 60, 90];
const expected = [1, 1, 0];
expect(dailyTemperatures(temperatures)).toStrictEqual(expected);
}
});
});
20 changes: 20 additions & 0 deletions src/page-7/739. Daily Temperatures/dailyTemperatures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type DailyTemperatures = (temperatures: number[]) => number[];

/**
* Accepted
*/
export const dailyTemperatures: DailyTemperatures = (temperatures) => {
const answer: number[] = Array(temperatures.length).fill(0);
const stack: number[] = [];

for (let i = 0; i < temperatures.length; i++) {
while (stack.length > 0 && temperatures[i] > temperatures[stack[stack.length - 1]]) {
const index = stack.pop() as number;
answer[index] = i - index;
}

stack.push(i);
}

return answer;
};

0 comments on commit 542cb4c

Please sign in to comment.