leet-code/best-time-to-buy-and-sell-s.../sol.rs

12 lines
332 B
Rust

impl Solution {
pub fn max_profit(prices: Vec<i32>) -> i32 {
prices
.iter()
.fold((prices[0], 0), |(min, max_diff), &price| {
if price < min {
return (price, max_diff);
}
(min, max_diff.max(price - min))
}).1
}
}