Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create: 0901-online-stock-span.ts
  • Loading branch information
AkifhanIlgaz committed Jan 4, 2023
commit 5f3b6d07ba9059ada827c4ff96aea0d1126ef6be
28 changes: 28 additions & 0 deletions typescript/0901-online-stock-span.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class StockSpanner {
private stack: { price: number; spanDays: number }[];

constructor() {
this.stack = [];
}

next(price: number): number {
let span = 1;

while (
this.stack.length > 0 &&
this.stack[this.stack.length - 1].price <= price
) {
span += this.stack[this.stack.length - 1].spanDays;
this.stack.pop();
}

this.stack.push({ price, spanDays: span });
return span;
}
}

/**
* Your StockSpanner object will be instantiated and called as such:
* var obj = new StockSpanner()
* var param_1 = obj.next(price)
*/