This is a loose continuation of my earlier challenge on constructing graphs.
Background
An eccentric artist has hired you to estimate the structural integrity of his sculptures. He creates his works of art by taking a bunch of cube-shaped magnets, and dropping them one by one into a huge pile. To better analyze his method, we use the following two-dimensional model. We start with an empty floor, and drop a magnet #
at any integer coordinate, say 0
:
|
v
#
===============
0
If another magnet is dropped at 0
, it ends up on top of the previous one:
|
v
#
#
===============
0
Now, let us drop one more magnet at 0
, and then one at 1
:
|
#v
##
#
===============
0
As seen above, a falling magnet sticks to the second magnet it passes (the first one merely slows it down). The second magnet need not be directly below the first one, and a magnet on both sides still counts as one magnet:
# #
##|##
# v #
### #
# #
===============
0
The artists wants you to calculate the maximal vertical gap in the final sculpture, that is, the maximum number of empty spaces between two magnets on the same column, or a magnet and the ground below it. In the above picture, this number would be 3 (on column 2
).
Input
A list of integers, representing the coordinates where the artist drops his magnets, read from left to right. You may assume that the coordinates satisfy -1024 <= i < 1024
and that the length of the list is at most 1024
, if that helps.
Output
The maximal vertical gap in the final sculpture. The empty sculpture has gap -1
, and this case has to be included, since our sculptor is a dadaist.
Additional rules
You may give a function or a full program. The shortest byte count wins, and standard loopholes are disallowed. Code with explanations is preferred.
Test cases
[] -> -1
[0,2,1] -> 0
[0,0,0,0,0,1,-1] -> 3
[0,0,0,0,0,1,1,1,2] -> 4
[1,1,2,2,2,2,2,2,1] -> 2
[1,1,2,2,2,2,2,2,1,0,1,0] -> 2
[1,2,1,2,1,2,1,2,2,2,2,1,0] -> 3
[-1,-1,-1,1,1,1,0] -> 1
[-1,-1,-1,-1,2,2,1,1,2,2,2,1,0] -> 2
[-2,-2,-2,-1,-1,-1,0,0,0,1,1,1,2,2,2,3,3,4,4,5,5,5,6] -> 6