lc-go/solutions/9/q901/solution.go
2026-01-25 11:56:12 +09:00

39 lines
687 B
Go

package q901
type Price struct {
day int
price int
}
type StockSpanner struct {
days []Price
currentDay int
}
func Constructor() StockSpanner { return StockSpanner{} }
func (s *StockSpanner) Next(price int) int {
s.currentDay++
curr := Price{day: s.currentDay, price: price}
i := len(s.days) - 1
for ; i >= 0; i-- {
if s.days[i].price > curr.price {
break
}
}
s.days = s.days[:i+1]
ret := s.currentDay
if len(s.days) > 0 {
ret = s.currentDay - s.days[len(s.days)-1].day
}
s.days = append(s.days, curr)
return ret
}
/**
* Your StockSpanner object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.Next(price);
*/