diff --git a/kotlin/121-Best-Time-to-Buy-and-Sell-Stocks.kt b/kotlin/121-Best-Time-to-Buy-and-Sell-Stocks.kt new file mode 100644 index 000000000..fcdfb05cb --- /dev/null +++ b/kotlin/121-Best-Time-to-Buy-and-Sell-Stocks.kt @@ -0,0 +1,11 @@ +class Solution { + fun maxProfit(prices: IntArray): Int { + var buy = prices[0] + var maxDiff = 0 + for (i in 1 until prices.size) { + buy = buy.coerceAtMost(prices[i]) + maxDiff = maxDiff.coerceAtLeast(prices[i] - buy) + } + return maxDiff + } +}