From cf381d1c0e446758b54b5ac9869540c8a7e8869f Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Tue, 31 Dec 2024 16:17:18 +0800 Subject: [PATCH] 260th Commit --- README.md | 3 ++- src/page-9/901. Online Stock Span/README.md | 13 ++++++++++ .../StockSpanner.test.ts | 14 ++++++++++ .../901. Online Stock Span/StockSpanner.ts | 26 +++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/page-9/901. Online Stock Span/README.md create mode 100644 src/page-9/901. Online Stock Span/StockSpanner.test.ts create mode 100644 src/page-9/901. Online Stock Span/StockSpanner.ts diff --git a/README.md b/README.md index a4721f5..1a84b75 100644 --- a/README.md +++ b/README.md @@ -295,9 +295,10 @@ Problems: | Monotonic Stack | | | | | ----------------------- | --------------- | ------ | ---- | | 739. Daily Temperatures | [Solution][739] | Medium | 詳解 | -| 901. Online Stock Span | Solution | Medium | 詳解 | +| 901. Online Stock Span | [Solution][901] | Medium | 詳解 | [739]: ./src/page-7/739.%20Daily%20Temperatures/dailyTemperatures.ts +[901]: ./src/page-9/901.%20Online%20Stock%20Span/StockSpanner.ts ## Advanced - Top Interview 150 diff --git a/src/page-9/901. Online Stock Span/README.md b/src/page-9/901. Online Stock Span/README.md new file mode 100644 index 0000000..2e6efd0 --- /dev/null +++ b/src/page-9/901. Online Stock Span/README.md @@ -0,0 +1,13 @@ +# 72. 股票價格跨距 (Online Stock Span) + +設計一個演算法,收集某些股票的每日報價並返回該股票當日價格的**跨距**。 + +當日股票價格的**跨距**是指股票價格小於或等於當日價格的最大連續天數(從該日開始往回算)。 + +- 例如,如果最近四天股票的價格是 `[7,2,1,2]`,今天股票的價格是 `2`,那麼今天的跨距是 `4`,因為從今天開始,股票的價格連續 `4` 天小於或等於 `2`。 +- 另外,如果最近四天股票的價格是 `[7,34,1,2]`,今天股票的價格是 `8`,那麼今天的跨距是 `3`,因為從今天開始,股票的價格連續 `3` 天小於或等於 `8`。 + +實作 `StockSpanner` 類別: + +- `StockSpanner()` 初始化類別的物件。 +- `next(price: number): number` 給出今天的股價 `price`,返回該股票價格的**跨距**。 diff --git a/src/page-9/901. Online Stock Span/StockSpanner.test.ts b/src/page-9/901. Online Stock Span/StockSpanner.test.ts new file mode 100644 index 0000000..53950e2 --- /dev/null +++ b/src/page-9/901. Online Stock Span/StockSpanner.test.ts @@ -0,0 +1,14 @@ +import { StockSpanner } from './StockSpanner'; + +describe('901. Online Stock Span', () => { + test('StockSpanner', () => { + const stockSpanner = new StockSpanner(); + expect(stockSpanner.next(100)).toBe(1); + expect(stockSpanner.next(80)).toBe(1); + expect(stockSpanner.next(60)).toBe(1); + expect(stockSpanner.next(70)).toBe(2); + expect(stockSpanner.next(60)).toBe(1); + expect(stockSpanner.next(75)).toBe(4); + expect(stockSpanner.next(85)).toBe(6); + }); +}); diff --git a/src/page-9/901. Online Stock Span/StockSpanner.ts b/src/page-9/901. Online Stock Span/StockSpanner.ts new file mode 100644 index 0000000..e788bf4 --- /dev/null +++ b/src/page-9/901. Online Stock Span/StockSpanner.ts @@ -0,0 +1,26 @@ +/** + * Accepted + */ +export class StockSpanner { + private stack: [number, number][]; + + constructor() { + // Initialize the stack, where each element is [price, span] + this.stack = []; + } + + next(price: number): number { + let span = 1; + + // While stack is not empty and the current price is greater than or equal to the price on top of the stack + while (this.stack.length > 0 && this.stack[this.stack.length - 1][0] <= price) { + // Pop the top of the stack and add its span to the current span + span += (this.stack.pop() as [number, number])[1]; + } + + // Push the current price and its span onto the stack + this.stack.push([price, span]); + + return span; + } +}