swift: add «901. Online Stock Span»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
97fecfe614
commit
c02a481db2
1 changed files with 23 additions and 0 deletions
23
swift/online-stock-span.swift
Normal file
23
swift/online-stock-span.swift
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
class StockSpanner {
|
||||||
|
private var st: [(Int, Int)] = []
|
||||||
|
|
||||||
|
init() {}
|
||||||
|
|
||||||
|
func next(_ price: Int) -> Int {
|
||||||
|
var span = 1
|
||||||
|
while !st.isEmpty && st.last!.0 <= price {
|
||||||
|
span += st.last!.1
|
||||||
|
st.removeLast()
|
||||||
|
}
|
||||||
|
|
||||||
|
st.append((price, span))
|
||||||
|
|
||||||
|
return span
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Your StockSpanner object will be instantiated and called as such:
|
||||||
|
* let obj = StockSpanner()
|
||||||
|
* let ret_1: Int = obj.next(price)
|
||||||
|
*/
|
Loading…
Reference in a new issue