Skip to content

Commit 5e615ba

Browse files
committed
ruby solution best time buy and sell stock
1 parent 8ad8f65 commit 5e615ba

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

best_time_to_buy_sell_stock.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def max_profit(prices)
2+
max_pro = 0
3+
min_price = prices.first
4+
prices.each do |cur_price|
5+
max_pro = [max_pro, cur_price-min_price].max
6+
min_price = [min_price, cur_price].min
7+
end
8+
return max_pro
9+
end
10+
11+
prices = [2,1,3,5,2,8,0,1,7,2]
12+
puts max_profit(prices)

0 commit comments

Comments
 (0)