Rust Max Slice Sum
fn max_slice_sum(a: &[i64]) -> i64 {
let mut tmp = a[0];
let mut max = a[0];
for &v in &a[1..] {
tmp = (tmp + v).max(v);
max = max.max(tmp);
}
max
}
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.
TypeScript Max Slice Sum
function maxSliceSum(a: number[]): number {
let tmp = -Infinity;
let max = -Infinity;
for (const v of a) {
tmp = Math.max(tmp + v, v);
max = Math.max(max, tmp);
}
return max;
}
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.
Bash Min Avg Two Slice
min_avg_two_slice() {
local -n _a="$1"
local _idx=0
local _min
_min=$(echo "scale=10; (${_a[0]} + ${_a[1]}) / 2" | bc)
local _count=${#_a[@]} _i
for ((_i = 0; _i < _count - 1; _i++)); do
local _cur
_cur=$(echo "scale=10; (${_a[_i]} + ${_a[_i+1]}) / 2" | bc)
if (( _i + 2 < _count )); then
local _three
_three=$(echo "scale=10; (${_a[_i]} + ${_a[_i+1]} + ${_a[_i+2]}) / 3" | bc)
if (( $(echo "$_three < $_cur" | bc -l) )); then
_cur=$_three
fi
fi
if (( $(echo "$_cur < $_min" | bc -l) )); then
_min=$_cur
_idx=$_i
fi
done
echo "$_idx"
}
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.
C++ Min Avg Two Slice
#include <algorithm>
#include <vector>
int minAvgTwoSlice(const std::vector<int>& a)
{
int idx = 0;
double min = (a[0] + a[1]) / 2.0;
for (std::size_t i = 0; i + 1 < a.size(); ++i) {
double cur = (a[i] + a[i + 1]) / 2.0;
if (i + 2 < a.size()) {
double three = (a[i] + a[i + 1] + a[i + 2]) / 3.0;
cur = std::min(cur, three);
}
if (cur < min) {
min = cur;
idx = static_cast<int>(i);
}
}
return idx;
}
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.
C# Min Avg Two Slice
static int MinAvgTwoSlice(int[] a)
{
var idx = 0;
double min = (a[0] + a[1]) / 2.0;
for (int i = 0; i < a.Length - 1; i++)
{
double cur = (a[i] + a[i + 1]) / 2.0;
if (i + 2 < a.Length)
{
double three = (a[i] + a[i + 1] + a[i + 2]) / 3.0;
cur = cur < three ? cur : three;
}
if (cur < min)
{
min = cur;
idx = i;
}
}
return idx;
}
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.
Elixir Min Avg Two Slice
defmodule MinAvgTwoSlice do
def min_avg_two_slice(a) do
size = length(a)
a_map = a |> Enum.with_index() |> Map.new(fn {v, i} -> {i, v} end)
initial_avg = (Map.get(a_map, 0) + Map.get(a_map, 1)) / 2
{idx, _min_avg} =
Enum.reduce(0..(size - 2), {0, initial_avg}, fn i, {idx, min_avg} ->
two = (Map.get(a_map, i) + Map.get(a_map, i + 1)) / 2
cur =
if Map.has_key?(a_map, i + 2) do
three = (Map.get(a_map, i) + Map.get(a_map, i + 1) + Map.get(a_map, i + 2)) / 3
min(two, three)
else
two
end
if cur < min_avg, do: {i, cur}, else: {idx, min_avg}
end)
idx
end
end
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.
Erlang Min Avg Two Slice
-module(min_avg_two_slice).
-export([min_avg_two_slice/1]).
min_avg_two_slice(A) ->
N = length(A),
Arr = array:from_list(A),
Init = (array:get(0, Arr) + array:get(1, Arr)) / 2,
{_, Idx} = lists:foldl(fun(I, {MinV, IdxAcc}) ->
Two = (array:get(I, Arr) + array:get(I + 1, Arr)) / 2,
Cur = case I + 2 < N of
true ->
Three = (array:get(I, Arr) + array:get(I + 1, Arr) + array:get(I + 2, Arr)) / 3,
min(Two, Three);
false ->
Two
end,
case Cur < MinV of
true -> {Cur, I};
false -> {MinV, IdxAcc}
end
end, {Init, 0}, lists:seq(0, N - 2)),
Idx.
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.
Go Min Avg Two Slice
func minAvgTwoSlice(a []int) int {
idx := 0
min := float64(a[0]+a[1]) / 2
for i := 0; i < len(a)-1; i++ {
cur := float64(a[i]+a[i+1]) / 2
if i+2 < len(a) {
three := float64(a[i]+a[i+1]+a[i+2]) / 3
if three < cur {
cur = three
}
}
if cur < min {
min = cur
idx = i
}
}
return idx
}
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.
Haskell Min Avg Two Slice
import Data.Array (Array, listArray, (!))
minAvgTwoSlice :: [Int] -> Int
minAvgTwoSlice a = fst (foldl step (0, avg2 0) [0 .. n - 2])
where
n = length a
arr = listArray (0, n - 1) a :: Array Int Int
avg2 i = fromIntegral (arr ! i + arr ! (i + 1)) / 2 :: Double
avg3 i = fromIntegral (arr ! i + arr ! (i + 1) + arr ! (i + 2)) / 3 :: Double
candidate i
| i + 2 <= n - 1 = min (avg2 i) (avg3 i)
| otherwise = avg2 i
step (bestIdx, bestVal) i =
let cur = candidate i
in if cur < bestVal then (i, cur) else (bestIdx, bestVal)
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.
Java Min Avg Two Slice
public class Solution {
public static int minAvgTwoSlice(int[] a) {
int idx = 0;
double min = (a[0] + a[1]) / 2.0;
for (int i = 0; i < a.length - 1; i++) {
double cur = (a[i] + a[i + 1]) / 2.0;
if (i + 2 < a.length) {
double three = (a[i] + a[i + 1] + a[i + 2]) / 3.0;
cur = Math.min(cur, three);
}
if (cur < min) {
min = cur;
idx = i;
}
}
return idx;
}
}
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.