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

12 lines
332 B
Rust
Raw Normal View History

2022-05-30 22:03:12 +00:00
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
}
}