Lisp Min Avg Two Slice
(defun min-avg-two-slice (a)
(let* ((vec (coerce a 'vector))
(count (length vec))
(idx 0)
(min (/ (+ (aref vec 0) (aref vec 1)) 2.0)))
(loop for i from 0 below (1- count)
do (let ((cur (/ (+ (aref vec i) (aref vec (1+ i))) 2.0)))
(when (< (+ i 2) count)
(let ((three (/ (+ (aref vec i) (aref vec (1+ i)) (aref vec (+ i 2))) 3.0)))
(setf cur (min cur three))))
(when (< cur min)
(setf min cur)
(setf idx i))))
idx))
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.
PHP Min Avg Two Slice
function minAvgTwoSlice(array $a): int
{
$idx = 0;
$min = ($a[0] + $a[1]) / 2;
for ($i = 0, $count = count($a); $i < $count - 1; $i++) {
$cur = ($a[$i] + $a[$i + 1]) / 2;
if (isset($a[$i + 2])) {
$three = ($a[$i] + $a[$i + 1] + $a[$i + 2]) / 3;
$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.
Python Min Avg Two Slice
def min_avg_two_slice(a: list[int]) -> int:
idx = 0
min_avg = (a[0] + a[1]) / 2
for i in range(len(a) - 1):
cur = (a[i] + a[i + 1]) / 2
if i + 2 < len(a):
three = (a[i] + a[i + 1] + a[i + 2]) / 3
cur = min(cur, three)
if cur < min_avg:
min_avg = cur
idx = i
return idx
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.
Rust Min Avg Two Slice
fn min_avg_two_slice(a: &[i64]) -> i64 {
let mut idx = 0i64;
let mut min = (a[0] + a[1]) as f64 / 2.0;
for i in 0..a.len() - 1 {
let mut cur = (a[i] + a[i + 1]) as f64 / 2.0;
if i + 2 < a.len() {
let three = (a[i] + a[i + 1] + a[i + 2]) as f64 / 3.0;
cur = cur.min(three);
}
if cur < min {
min = cur;
idx = i as i64;
}
}
idx
}
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.
TypeScript Min Avg Two Slice
function minAvgTwoSlice(a: number[]): number {
let idx = 0;
let min = (a[0] + a[1]) / 2;
for (let i = 0; i < a.length - 1; i++) {
let cur = (a[i] + a[i + 1]) / 2;
if (a[i + 2] !== undefined) {
const three = (a[i] + a[i + 1] + a[i + 2]) / 3;
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.
Bash Min Perimeter Rectangle
min_perimeter_rectangle() {
local _n=$1
local _i=1
local _min=9223372036854775807
while (( _i * _i < _n )); do
if (( _n % _i == 0 )); then
local _perim=$(( 2 * (_i + _n / _i) ))
(( _perim < _min )) && _min=$_perim
fi
((_i++))
done
echo "$_min"
}
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
C++ Min Perimeter Rectangle
#include <algorithm>
#include <limits>
long long minPerimeterRectangle(long long n)
{
long long i = 1;
long long min = std::numeric_limits<long long>::max();
while (i * i < n) {
if (n % i == 0) {
min = std::min(min, 2 * (i + n / i));
}
++i;
}
return min;
}
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
C# Min Perimeter Rectangle
static long MinPerimeterRectangle(long n)
{
long i = 1;
long min = long.MaxValue;
while (i * i < n)
{
if (n % i == 0)
{
min = Math.Min(min, 2 * (i + n / i));
}
i++;
}
return min;
}
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
Elixir Min Perimeter Rectangle
defmodule MinPerimeterRectangle do
def min_perimeter_rectangle(n), do: loop(1, n, :infinity)
defp loop(i, n, min) when i * i < n do
candidate = if rem(n, i) == 0, do: 2 * (i + div(n, i)), else: nil
min =
cond do
is_nil(candidate) -> min
min == :infinity -> candidate
candidate < min -> candidate
true -> min
end
loop(i + 1, n, min)
end
defp loop(_i, _n, min), do: min
end
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
Erlang Min Perimeter Rectangle
-module(min_perimeter_rectangle).
-export([min_perimeter_rectangle/1]).
min_perimeter_rectangle(N) ->
find_min(1, N, 1 bsl 128).
find_min(I, N, Min) when I * I >= N ->
Min;
find_min(I, N, Min) ->
Min1 = case N rem I of
0 -> min(Min, 2 * (I + (N div I)));
_ -> Min
end,
find_min(I + 1, N, Min1).
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.