LeetCode 121. Best Time to Buy and Sell Stock in F#
URL
leetcode.com/problems/best-time-to-buy-and-..
Code
github.com/syohex/dotnet-study/blob/master/..
let maxProfit (prices: int list) : int =
let rec maxProfit' prices min max =
match prices with
| [] -> max
| p :: ps ->
let newMin = if p < min then p else min
let profit = p - newMin
let newMax = if profit > max then profit else max
maxProfit' ps newMin newMax
maxProfit' prices System.Int32.MaxValue 0