Shorter version of Skyscrapers Challenge
Task
Given an array of building heights and a positive integer k
, find all the permutations(without duplicates) of the heights such that exactly k
buildings are visible.
Any building will hide all shorter or equal height buildings behind it.
Any format for input and output is valid.
Input array will never be empty.
In case it is not possible to see exactly as many buildings, output anything that cannot be an answer but no error.
Examples:
(Length of output is shown for very long outputs,but your output should be all possible permutations)
input:[1,2,3,4,5],2
output: 50
input:[5,5,5,5,5,5,5,5],2
output: []
input:[1,2,2],2
output:[(1,2,2)]
Seeing from the left, exactly 2 buildings are visible.
input:[1,7,4],2
output:[(4, 7, 1), (1, 7, 4), (4, 1, 7)]
input:[1,2,3,4,5,6,7,8,9],4
output:67284
input:[34,55,11,22],1
output:[(55, 34, 11, 22), (55, 22, 34, 11), (55, 34, 22, 11), (55, 11, 34, 22), (55, 22, 11, 34), (55, 11, 22, 34)]
input:[3,4,1,2,3],2
output:31
This is code-golf so shortest code wins
Optional: If possible, can you add something like if length is greater than 20: print length else print answer
. In the footer, not in the code.
[1,2,3,4,5],5 -> [(1,2,3,4,5)]
. None of the current test cases ensure that answers can support showing all buildings (though I don't know whether any actually have a problem with that). \$\endgroup\$