PHP Stone Blocks
function stoneBlocks(array $h): int
{
$height = [];
$index = $blocks = 0;
foreach ($h as $i) {
while ($index > 0 && $height[$index - 1] > $i) {
$index--;
}
if ($index > 0 && $height[$index - 1] === $i) {
continue;
}
$height[$index] = $i;
$blocks++;
$index++;
}
return $blocks;
}
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.
Python Stone Blocks
def stone_blocks(h: list[int]) -> int:
height: list[int] = []
index = 0
blocks = 0
for i in h:
while index > 0 and height[index - 1] > i:
index -= 1
if index > 0 and height[index - 1] == i:
continue
if index < len(height):
height[index] = i
else:
height.append(i)
blocks += 1
index += 1
return blocks
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.
Rust Stone Blocks
fn stone_blocks(h: &[i64]) -> i64 {
let mut height: Vec<i64> = Vec::new();
let mut blocks = 0i64;
for &i in h {
while let Some(&last) = height.last() {
if last > i {
height.pop();
} else {
break;
}
}
if let Some(&last) = height.last() {
if last == i {
continue;
}
}
height.push(i);
blocks += 1;
}
blocks
}
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.
TypeScript Stone Blocks
function stoneBlocks(h: number[]): number {
const height: number[] = [];
let index = 0;
let blocks = 0;
for (const i of h) {
while (index > 0 && height[index - 1] > i) {
index--;
}
if (index > 0 && height[index - 1] === i) {
continue;
}
height[index] = i;
blocks++;
index++;
}
return blocks;
}
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.
Bash Tape Equilibrium
tape_equilibrium() {
local -n _a="$1"
local _firstPart=0
local _secondPart=0
local _v
for _v in "${_a[@]}"; do _secondPart=$(( _secondPart + _v )); done
local _min=9223372036854775807
local _i
for ((_i = 0; _i < ${#_a[@]} - 1; _i++)); do
_firstPart=$(( _firstPart + _i ))
_secondPart=$(( _secondPart - _i ))
local _diff=$(( _firstPart - _secondPart ))
(( _diff < 0 )) && _diff=$(( -_diff ))
(( _diff < _min )) && _min=$_diff
done
echo "$_min"
}
This keeps left and right running sums and updates the smallest difference at each split point.
C++ Tape Equilibrium
#include <cstdlib>
#include <limits>
#include <numeric>
#include <vector>
long long tapeEquilibrium(const std::vector<int>& a)
{
long long firstPart = 0;
long long secondPart = std::accumulate(a.begin(), a.end(), 0LL);
long long min = std::numeric_limits<long long>::max();
for (std::size_t i = 0; i + 1 < a.size(); ++i) {
// index `i` rather than `a[i]` here.
firstPart += static_cast<long long>(i);
secondPart -= static_cast<long long>(i);
long long difference = std::llabs(firstPart - secondPart);
min = std::min(min, difference);
}
return min;
}
This keeps left and right running sums and updates the smallest difference at each split point.
C# Tape Equilibrium
static long TapeEquilibrium(int[] a)
{
long firstPart = 0;
long secondPart = a.Sum(x => (long)x);
long min = long.MaxValue;
for (int i = 0; i < a.Length - 1; i++)
{
firstPart += i;
secondPart -= i;
var difference = Math.Abs(firstPart - secondPart);
min = difference < min ? difference : min;
}
return min;
}
This keeps left and right running sums and updates the smallest difference at each split point.
Elixir Tape Equilibrium
defmodule TapeEquilibrium do
def tape_equilibrium(a) do
total = Enum.sum(a)
size = length(a)
{_first, _second, min} =
Enum.reduce(0..(size - 2)//1, {0, total, :infinity}, fn i, {first, second, min} ->
first = first + i
second = second - i
diff = abs(first - second)
min = if min == :infinity or diff < min, do: diff, else: min
{first, second, min}
end)
min
end
end
This keeps left and right running sums and updates the smallest difference at each split point.
Erlang Tape Equilibrium
-module(tape_equilibrium).
-export([tape_equilibrium/1]).
%% Faithful port of the PHP source: it accumulates the loop index i into
%% firstPart/secondPart rather than a[i], so this mirrors that behaviour
%% (including the quirk) rather than the classic tape-equilibrium formula.
tape_equilibrium(A) ->
N = length(A),
Total = lists:sum(A),
{_, _, Min} = lists:foldl(fun(I, {First, Second, MinV}) ->
First1 = First + I,
Second1 = Second - I,
Diff = abs(First1 - Second1),
{First1, Second1, min(MinV, Diff)}
end, {0, Total, 1 bsl 128}, lists:seq(0, N - 2)),
Min.
This keeps left and right running sums and updates the smallest difference at each split point.
Go Tape Equilibrium
func abs(n int) int {
if n < 0 {
return -n
}
return n
}
func tapeEquilibrium(a []int) int {
firstPart := 0
secondPart := 0
for _, v := range a {
secondPart += v
}
min := math.MaxInt
for i := 0; i < len(a)-1; i++ {
firstPart += i
secondPart -= i
if diff := abs(firstPart - secondPart); diff < min {
min = diff
}
}
return min
}
This keeps left and right running sums and updates the smallest difference at each split point.