add new solutions
This commit is contained in:
parent
886b5e0a8e
commit
67cad91898
47 changed files with 1549 additions and 1 deletions
39
solutions/9/q901/solution.go
Normal file
39
solutions/9/q901/solution.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
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);
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue