A tower is made out of layers, each one being one unit shorter than the one below it. Every layer is completely on top of the previous layer. For example, here is a tower along with it's height map:
#
##
###
####
#####
14532
Because the lengths of the layers are the integers from 1 to n, and because the layers are completely on top of each other, the height map will always be a permutation of the integers from 1
to n
. (Can you see why? Comment below)
The converse is not true. Some permutations are not the height map of a tower, meaning they are not tower permutations. For example, [2,1,3,5,4]
is not the height map of any tower, meaning that it's not a tower permutation. However, [1,4,5,3,2]
is a tower permutation, as you can see from the previous ascii drawing.
Just to be clear, the following is not a tower:
#
##
###
# ###
#####
21354
Because each layer has to be continuous. This is instead a castle :P
Your task is to take a permutation of the integers 1 to n (inclusive) and decide if it's a tower permutation, as per usual code-golf and decision-problem rules. You can assume that n>0
Test cases (all permutations up to n=4)
[1] -> True
[1, 2] -> True
[2, 1] -> True
[1, 2, 3] -> True
[1, 3, 2] -> True
[2, 1, 3] -> False
[2, 3, 1] -> True
[3, 1, 2] -> False
[3, 2, 1] -> True
[1, 2, 3, 4] -> True
[1, 2, 4, 3] -> True
[1, 3, 2, 4] -> False
[1, 3, 4, 2] -> True
[1, 4, 2, 3] -> False
[1, 4, 3, 2] -> True
[2, 1, 3, 4] -> False
[2, 1, 4, 3] -> False
[2, 3, 1, 4] -> False
[2, 3, 4, 1] -> True
[2, 4, 1, 3] -> False
[2, 4, 3, 1] -> True
[3, 1, 2, 4] -> False
[3, 1, 4, 2] -> False
[3, 2, 1, 4] -> False
[3, 2, 4, 1] -> False
[3, 4, 1, 2] -> False
[3, 4, 2, 1] -> True
[4, 1, 2, 3] -> False
[4, 1, 3, 2] -> False
[4, 2, 1, 3] -> False
[4, 2, 3, 1] -> False
[4, 3, 1, 2] -> False
[4, 3, 2, 1] -> True