Skip to content

Commit 7daded7

Browse files
authored
Merge pull request neetcode-gh#1886 from AkifhanIlgaz/0901
Create: 0901-online-stock-span.ts
2 parents 1b59aab + 5f3b6d0 commit 7daded7

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

typescript/0901-online-stock-span.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class StockSpanner {
2+
private stack: { price: number; spanDays: number }[];
3+
4+
constructor() {
5+
this.stack = [];
6+
}
7+
8+
next(price: number): number {
9+
let span = 1;
10+
11+
while (
12+
this.stack.length > 0 &&
13+
this.stack[this.stack.length - 1].price <= price
14+
) {
15+
span += this.stack[this.stack.length - 1].spanDays;
16+
this.stack.pop();
17+
}
18+
19+
this.stack.push({ price, spanDays: span });
20+
return span;
21+
}
22+
}
23+
24+
/**
25+
* Your StockSpanner object will be instantiated and called as such:
26+
* var obj = new StockSpanner()
27+
* var param_1 = obj.next(price)
28+
*/

0 commit comments

Comments
 (0)