In this challenge you will receive a list of non-negative integers. Each one represents a mushroom with a cap of that radius centered at that location. So a 0
means that it occupies no space at all, a 1
means that its cap only occupies space above it, a 2
means it occupies space above it and one unit to the left and right etc. More generally for size \$n\$ a total of \$2n-1\$ spaces are occupied, with the exception of 0 which occupies 0 spaces (i.e. there's no mushroom at all).
Mushroom caps can't occupy the same space as each other but mushrooms can have different heights to avoid collisions.
So for example here we have two mushrooms. They can't be in the same row since they would occupy the same space but if they are given different heights there is no issue:
=-=-=
|
=-=-=-=-= |
| |
[ 0,0,3,0,0,2,0 ]
(Stems are drawn with |
for clarity, but can't collide)
Your task is to take as input a list of mushrooms and output a list of heights, one for each input mushroom, such that there is no collisions between mushrooms and they occupy the fewest total rows.
For example here we have a worked case:
=-=-= =-=-=-=-=-=-= =
| | |
| =-=-= | =-=-=
| | | | |
=-=-= =-=-=-=-=
| | | | | | |
[ 2,2,2,0,0,4,0,3,2,1 ] <- Widths
[ 2,0,1,0,0,2,0,0,1,2 ] -> Heights
(Stems are drawn with |
, and extra spacer rows are added between layers for clarity)
For any input there are a wide variety of valid answers, you are allowed to output any 1 of them or all of them. You may also consistently use 1-indexed heights instead of 0-indexed heights if you wish.
This is code-golf so the goal will be to minimize the size of your source code as measured in bytes.
Selected examples
Here are some selected examples with possible solutions:
This one defeats a certain greedy algorithm:
=-=-= =
| |
=-=-= =-=-=-=-=
| | | |
[ 2,2,0,1,3 ] <- Width
[ 0,1,0,1,0 ] -> Height
This one requires everything to be on its own row:
=-=-=-=-=-=-=
|
=-=-=-=-=-=-=
| |
=-=-= |
| | |
= | | |
| | | |
=-=-= | | |
| | | | |
[ 2,1,2,4,4 ] <- Width
[ 0,1,2,3,4 ] -> Height
[3, 1, 2, null, null, 3, null, 1, 2, 3]
(or something like it) instead. \$\endgroup\$[0,2,2]
you can put the0
at height0
or1
but not2
or larger. Put another way: they should be on row0
if there are only0
s in the input, and otherwise they can be in any row that another non-0
occupies. \$\endgroup\$