-
lang
kotlin
-
tags
Array
DP
import kotlin.math.max
class Solution {
fun maxProfit(prices: IntArray): Int {
// check max value of sell and cooldown
var sell = 0
var buy = -prices[0]
var cooldown = 0
var minBuy = buy
var tmp = sell
// iterate
for (i in 1..prices.size-1) {
// get each sell-buy pair and cooldowns for max profit.
sell = minBuy + prices[i]
buy = cooldown - prices[i]
cooldown = max(cooldown, tmp)
tmp = sell
minBuy = max(minBuy, buy)
}
return max(sell, cooldown)
}
}