Skip to content

Commit

Permalink
Create: 309-Best-Time-to-Buy-and-Sell-Stock-with-Cooldown.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
Ykhan799 authored Sep 14, 2022
1 parent 582de48 commit 636ce11
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions kotlin/309-Best-Time-to-Buy-and-Sell-Stock-with-Cooldown.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
fun maxProfit(prices: IntArray): Int {
var sold = 0
var hold = Int.MIN_VALUE
var rest = 0

for (i in 0..prices.size-1) {
val prevSold = sold
sold = hold + prices[i]
hold = maxOf(hold, rest - prices[i])
rest = maxOf(rest, prevSold)
}
return maxOf(sold, rest)
}
}

0 comments on commit 636ce11

Please sign in to comment.