Skip to content

Commit 0483b96

Browse files
authored
Merge pull request #586 from edo-ce/main
Add Kotlin 121-Best-Time-to-Buy-and-Sell-Stocks
2 parents 981adc7 + 883c4df commit 0483b96

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
fun maxProfit(prices: IntArray): Int {
3+
var buy = prices[0]
4+
var maxDiff = 0
5+
for (i in 1 until prices.size) {
6+
buy = buy.coerceAtMost(prices[i])
7+
maxDiff = maxDiff.coerceAtLeast(prices[i] - buy)
8+
}
9+
return maxDiff
10+
}
11+
}

0 commit comments

Comments
 (0)