Erlang Max Profit
-module(max_profit).
-export([max_profit/1]).
max_profit([H | T]) ->
{_, Profit} = lists:foldl(fun(V, {MinPrice, MaxProfit}) ->
MinPrice1 = min(MinPrice, V),
{MinPrice1, max(MaxProfit, V - MinPrice1)}
end, {H, 0}, T),
Profit.
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.
Go Max Profit
func maxProfit(a []int) int {
price := a[0]
profit := 0
for _, v := range a {
if v < price {
price = v
}
if v-price > profit {
profit = v - price
}
}
return profit
}
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.
Haskell Max Profit
maxProfit :: [Int] -> Int
maxProfit a = snd (foldl step (head a, 0) a)
where
step (price, profit) v =
let price' = min price v
in (price', max profit (v - price'))
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.
Java Max Profit
public class Solution {
public static int maxProfit(int[] a) {
int price = a[0];
int profit = 0;
for (int v : a) {
price = Math.min(price, v);
profit = Math.max(profit, v - price);
}
return profit;
}
}
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.
Lisp Max Profit
(defun max-profit (a)
(let ((price (first a))
(profit 0))
(dolist (v a)
(setf price (min price v))
(setf profit (max profit (- v price))))
profit))
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.
PHP Max Profit
function maxProfit(array $a): int
{
$price = $a[0];
$profit = 0;
foreach ($a as $v) {
$price = min($price, $v);
$profit = max($profit, $v - $price);
}
return $profit;
}
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.
Python Max Profit
def max_profit(a: list[int]) -> int:
price = a[0]
profit = 0
for v in a:
price = min(price, v)
profit = max(profit, v - price)
return profit
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.
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.
TypeScript Max Profit
function maxProfit(a: number[]): number {
let price = a[0];
let profit = 0;
for (const v of a) {
price = Math.min(price, v);
profit = Math.max(profit, v - price);
}
return profit;
}
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.
Bash Max Slice Sum
max_slice_sum() {
local -n _arr="$1"
local _tmp=-9223372036854775808 _max=-9223372036854775808
local _v
for _v in "${_arr[@]}"; do
if (( _tmp + _v > _v )); then _tmp=$(( _tmp + _v )); else _tmp=$_v; fi
if (( _tmp > _max )); then _max=$_tmp; fi
done
echo "$_max"
}
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.