g0901_1000.s0901_online_stock_span.StockSpanner.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0901_1000.s0901_online_stock_span
// #Medium #Stack #Design #Monotonic_Stack #Data_Stream
// #2023_04_13_Time_641_ms_(75.00%)_Space_66.5_MB_(10.71%)
import java.util.Deque
import java.util.LinkedList
class StockSpanner {
private val map: MutableMap
private val stocks: Deque
private var index: Int
init {
stocks = LinkedList()
index = -1
map = HashMap()
stocks.push(-1)
}
fun next(price: Int): Int {
if (index != -1) {
stocks.push(index)
}
map[++index] = price
if (stocks.size == 1) {
return index - stocks.peek()
}
while (stocks.size > 1 && map.getValue(stocks.peek()) <= price) {
stocks.pop()
}
return index - stocks.peek()
}
}
/*
* Your StockSpanner object will be instantiated and called as such:
* var obj = StockSpanner()
* var param_1 = obj.next(price)
*/