TypeScript Shape Area
function shapeArea(n: number): number {
return n > 1 ? shapeArea(n - 1) + 4 * (n - 1) : 1;
}
This returns the area of the growing n-interesting polygon using the direct formula instead of building the shape.
Bash Stone Blocks
stone_blocks() {
local -n _h="$1"
local -a _height
local _index=0 _blocks=0
local _i
for _i in "${_h[@]}"; do
while (( _index > 0 && _height[_index-1] > _i )); do
((_index--))
done
if (( _index > 0 && _height[_index-1] == _i )); then
continue
fi
_height[_index]=$_i
((_blocks++))
((_index++))
done
echo "$_blocks"
}
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.
C++ Stone Blocks
#include <vector>
int stoneBlocks(const std::vector<int>& h)
{
std::vector<int> height;
int index = 0;
int blocks = 0;
for (int i : h) {
while (index > 0 && height[index - 1] > i) {
--index;
}
if (index > 0 && height[index - 1] == i) {
continue;
}
if (index == static_cast<int>(height.size())) {
height.push_back(i);
} else {
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.
C# Stone Blocks
static int StoneBlocks(int[] h)
{
var height = new List<int>();
var index = 0;
var blocks = 0;
foreach (var i in h)
{
while (index > 0 && height[index - 1] > i)
{
index--;
}
if (index > 0 && height[index - 1] == i)
{
continue;
}
if (index < height.Count)
{
height[index] = i;
}
else
{
height.Add(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.
Elixir Stone Blocks
defmodule StoneBlocks do
def stone_blocks(h) do
{_stack, blocks} =
Enum.reduce(h, {[], 0}, fn height, {stack, blocks} ->
stack = Enum.drop_while(stack, &(&1 > height))
case stack do
[^height | _] -> {stack, blocks}
_ -> {[height | stack], blocks + 1}
end
end)
blocks
end
end
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.
Erlang Stone Blocks
-module(stone_blocks).
-export([stone_blocks/1]).
stone_blocks(H) ->
{_, Blocks} = lists:foldl(fun(Hi, {Stack, BlocksCount}) ->
Stack1 = pop_while_greater(Stack, Hi),
case Stack1 of
[Hi | _] -> {Stack1, BlocksCount};
_ -> {[Hi | Stack1], BlocksCount + 1}
end
end, {[], 0}, H),
Blocks.
pop_while_greater([Top | Rest], Hi) when Top > Hi ->
pop_while_greater(Rest, Hi);
pop_while_greater(Stack, _Hi) ->
Stack.
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.
Go Stone Blocks
func stoneBlocks(h []int) int {
height := make([]int, 0, len(h))
blocks := 0
for _, v := range h {
for len(height) > 0 && height[len(height)-1] > v {
height = height[:len(height)-1]
}
if len(height) > 0 && height[len(height)-1] == v {
continue
}
height = append(height, v)
blocks++
}
return blocks
}
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.
Haskell Stone Blocks
stoneBlocks :: [Int] -> Int
stoneBlocks h = snd (foldl step ([], 0) h)
where
step (stack, blocks) i =
case dropWhile (> i) stack of
(top : rest) | top == i -> (top : rest, blocks)
popped -> (i : popped, blocks + 1)
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.
Java Stone Blocks
public class Solution {
public static int stoneBlocks(int[] h) {
int[] height = new int[h.length];
int index = 0;
int blocks = 0;
for (int i : 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.
Lisp Stone Blocks
(defun stone-blocks (h)
(let* ((n (length h))
(height (make-array n :initial-element 0))
(index 0)
(blocks 0))
(dolist (i h)
(loop while (and (> index 0) (> (aref height (1- index)) i))
do (decf index))
(unless (and (> index 0) (= (aref height (1- index)) i))
(setf (aref height index) i)
(incf blocks)
(incf index)))
blocks))
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.