Skip to content

Commit ec69ac3

Browse files
Merge pull request #2948 from HasnainMG/main
Create: 121-Best-Time-to-Buy-and-Sell-Stock.scala
2 parents fe024f0 + 0fe14e0 commit ec69ac3

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
object Solution {
2+
def maxProfit(prices: Array[Int]): Int = {
3+
var left = 0
4+
var right = 1
5+
var maxProfit = 0
6+
while (right < prices.length) {
7+
val priceDiff = prices(right) - prices(left)
8+
if (priceDiff > 0) {
9+
maxProfit = maxProfit max priceDiff
10+
}
11+
else {
12+
left = right
13+
}
14+
right += 1
15+
}
16+
maxProfit
17+
}
18+
}

0 commit comments

Comments
 (0)