Skip to content

Commit

Permalink
Add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Dec 15, 2023
1 parent 1b1aecb commit c40217e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
29 changes: 29 additions & 0 deletions leetcode/design/901.OnlineStockSpan/onlineStockSpan.go
Original file line number Diff line number Diff line change
@@ -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);
*/
Original file line number Diff line number Diff line change
@@ -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);
*/

0 comments on commit c40217e

Please sign in to comment.