Rust Max Profit
fn max_profit(a: &[i64]) -> i64 {
let mut price = a[0];
let mut profit = 0i64;
for &v in a {
price = price.min(v);
profit = profit.max(v - price);
}
profit
}
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.