From c40217ed82757b16d0bb0f207309ebfd7d399477 Mon Sep 17 00:00:00 2001 From: ductnn Date: Fri, 15 Dec 2023 11:36:20 +0700 Subject: [PATCH] Add sol --- .../901.OnlineStockSpan/onlineStockSpan.go | 29 +++++++++++++++++++ .../901.OnlineStockSpan/onlineStockSpan.go | 29 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 leetcode/design/901.OnlineStockSpan/onlineStockSpan.go create mode 100644 leetcode/leetcode75/MonotonicStack/901.OnlineStockSpan/onlineStockSpan.go diff --git a/leetcode/design/901.OnlineStockSpan/onlineStockSpan.go b/leetcode/design/901.OnlineStockSpan/onlineStockSpan.go new file mode 100644 index 0000000..4eabeff --- /dev/null +++ b/leetcode/design/901.OnlineStockSpan/onlineStockSpan.go @@ -0,0 +1,29 @@ +package main + +type pair struct { + price, cnt int +} + +type StockSpanner struct { + stk []pair +} + +func Constructor() StockSpanner { + return StockSpanner{[]pair{}} +} + +func (this *StockSpanner) Next(price int) int { + cnt := 1 + for len(this.stk) > 0 && this.stk[len(this.stk)-1].price <= price { + cnt += this.stk[len(this.stk)-1].cnt + this.stk = this.stk[:len(this.stk)-1] + } + this.stk = append(this.stk, pair{price, cnt}) + return cnt +} + +/** + * Your StockSpanner object will be instantiated and called as such: + * obj := Constructor(); + * param_1 := obj.Next(price); + */ diff --git a/leetcode/leetcode75/MonotonicStack/901.OnlineStockSpan/onlineStockSpan.go b/leetcode/leetcode75/MonotonicStack/901.OnlineStockSpan/onlineStockSpan.go new file mode 100644 index 0000000..4eabeff --- /dev/null +++ b/leetcode/leetcode75/MonotonicStack/901.OnlineStockSpan/onlineStockSpan.go @@ -0,0 +1,29 @@ +package main + +type pair struct { + price, cnt int +} + +type StockSpanner struct { + stk []pair +} + +func Constructor() StockSpanner { + return StockSpanner{[]pair{}} +} + +func (this *StockSpanner) Next(price int) int { + cnt := 1 + for len(this.stk) > 0 && this.stk[len(this.stk)-1].price <= price { + cnt += this.stk[len(this.stk)-1].cnt + this.stk = this.stk[:len(this.stk)-1] + } + this.stk = append(this.stk, pair{price, cnt}) + return cnt +} + +/** + * Your StockSpanner object will be instantiated and called as such: + * obj := Constructor(); + * param_1 := obj.Next(price); + */