Postcard Mountain Skylines
A designer draws mountain skylines for postcards. The pen moves left to right in equal strokes, each up a slope (U) or down one (D). A skyline starts and ends on the horizon and never dips below it. The card is short, so no point may rise more than maxHeight strokes above the horizon.
For a fixed number of up-strokes, the designer wants one postcard per skyline that fits. UUDD is one tall peak; UDUD is two small hills.
Your task
Write allSkylines(climbs, maxHeight). Return every string made of climbs letters U and climbs letters D where, reading left to right, the height (number of Us so far minus number of Ds so far) never drops below 0 and never goes above maxHeight.
Return the strings in any order, each exactly once. If no skyline fits, return an empty list. For climbs = 0 the only skyline is the flat horizon, the empty string, so return [""].
allSkylines(climbs: int, maxHeight: int) → string[] Any order of the returned list is accepted.
Examples
climbs = 2 maxHeight = 2
["UUDD", "UDUD"]
Two Us and two Ds can be arranged in 6 ways, but only 2 stay above the horizon. For example, UDDU dips below on its third stroke and DUUD on its first. Both valid skylines reach at most height 2.
climbs = 3 maxHeight = 2
["UUDUDD", "UUDDUD", "UDUUDD", "UDUDUD"]
Without a height limit there would be 5 skylines. The fifth, UUUDDD, climbs to height 3, taller than the card allows.
climbs = 0 maxHeight = 0
[""]
With no strokes there is still one postcard, the flat horizon, which is the empty string.
Constraints
- 0 ≤ climbs ≤ 8
- 0 ≤ maxHeight ≤ 8
- Strings may be returned in any order.
Start with brute force
Generate all 22n strings of U and D and keep those that stay between the horizon and the limit and end on the horizon. For climbs = 8 that is 65,536 strings to keep at most 1,430, and the waste grows with n.
Most rejected strings are already invalid after a stroke or two, and the strings starting with D alone are half of all strings. Checking only at the end means building each one in full first.
Hints
0 of 4 shown- Hint 1
Write out the skylines for
climbs = 2by hand. Which first stroke is never allowed, and why? - Hint 2
Track the height as you draw. An up-stroke is allowed when up-strokes remain and the height is below
maxHeight. A down-stroke is allowed only when the height is above 0. These two rules are the only ones needed. - Hint 3
So build each skyline one stroke at a time, adding only allowed strokes. A recursive helper
draw(sky, upsLeft, height)triesUand thenD, each only when its rule allows. - Hint 4
When the string reaches length
2 × climbs, record it without checking: all up-strokes are used, so all down-strokes are too, and the height is 0. Passingsky + 'U'builds a new string, so there is nothing to undo after the call.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Instead of building strings and rejecting bad ones, never take a stroke that breaks a rule. Two numbers decide which stroke is allowed next, the number of up-strokes left and the current height.
Trace climbs = 2, maxHeight = 2. Each state is written as (sky, upsLeft, height):
- At
("", 2, 0)the height is 0, so onlyUis allowed. - At
("U", 1, 1)both strokes are allowed, and the function triesUfirst. - At
("UU", 0, 2)no up-strokes are left and the card's top is reached, so onlyDis allowed, twice in a row. The function recordsUUDD. - Back at
("U", 1, 1), the function triesDand reaches("UD", 1, 0). The height is 0, so onlyUis allowed, followed byD, and the function recordsUDUD.
With maxHeight = 1, the second line would allow only D, and UUDD would never start. While maxHeight is at least 1, every partial skyline can still be finished, so no work is wasted. A common mistake is to check only at the end that the counts match, which lets DU through.
function allSkylines(climbs, maxHeight) {
const skylines = [];
function draw(sky, upsLeft, height) {
if (sky.length === 2 * climbs) {
skylines.push(sky); // complete, and back on the horizon
return;
}
if (upsLeft > 0 && height < maxHeight) draw(sky + 'U', upsLeft - 1, height + 1);
if (height > 0) draw(sky + 'D', upsLeft, height - 1); // stay above
}
draw('', climbs, 0);
return skylines;
}def all_skylines(climbs, max_height):
skylines = []
def draw(sky, ups_left, height):
if len(sky) == 2 * climbs:
skylines.append(sky) # complete, and back on the horizon
return
if ups_left > 0 and height < max_height: # room to climb
draw(sky + 'U', ups_left - 1, height + 1)
if height > 0: # only go down if above the horizon
draw(sky + 'D', ups_left, height - 1)
draw('', climbs, 0)
return skylinesRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.