Code Logo

Celestial Skyline Layout

Published at05 Jan 2026
Multi Dimensional Easy 14 views
Like18

This challenge becomes much easier once you know exactly what to keep, change, or count. In Celestial Skyline Layout, you are trying to work toward the right number by following one clear idea.

Build recursive skyline layout structure A good way to think about it is to first understand what goes in, then what rule you must follow, and finally what shape the answer should have.

For example, if the input is blueprint = 4, the answer is 1. Example with input: blueprint = 4 Another example is blueprint = {"base": {"base": 5, "steps": [1]}, "steps": [2, 3]}, which gives 8. Example with input: blueprint = {"base": {"base": 5, "steps": [1]}, "sThis is a friendly practice problem, but it still rewards careful reading. The key is understanding the rule clearly and then applying it carefully.

One helpful habit is to say the rule out loud in your own words before you start solving. If you can explain what counts, what changes, and what the final answer should look like, you are already much closer to the right solution.

Example Input & Output

Example 1
Input
blueprint = 4
Output
1
Explanation

Example with input: blueprint = 4

Example 2
Input
blueprint = {"base": {"base": 5, "steps": [1]}, "steps": [2, 3]}
Output
8
Explanation

Example with input: blueprint = {"base": {"base": 5, "steps": [1]}, "s

Example 3
Input
blueprint = {"base": 4, "steps": [2]}
Output
2 (either use only the height-4 tower, or add a mirrored tower of height 6)
Explanation

Example with input: blueprint = {"base": 4, "steps": [2]}

Algorithm Flow

Recommendation Algorithm Flow for Celestial Skyline Layout

Solution Approach

This problem is a recursive counting exercise over a nested blueprint structure. The blueprint is either a plain number or an object with a base field and an optional steps array. We need to compute the number of valid skylines using a simple recursive rule.

The rule is: a plain number blueprint counts as exactly 1, and an object blueprint counts as the value of its base multiplied by 2 raised to the number of steps. Since the answer can grow very large, we take the result modulo 1e9 + 7.

Here is the implementation:

function count_valid_skylines(blueprint) {
    const MOD = 1000000007;
    if (typeof blueprint === "number") {
        return 1;
    }
    const baseCount = count_valid_skylines(blueprint.base);
    const k = (blueprint.steps || []).length;
    let pow2k = 1, b = 2;
    let exp = k;
    while (exp > 0) {
        if (exp % 2 === 1) pow2k = Number((BigInt(pow2k) * BigInt(b)) % BigInt(MOD));
        b = Number((BigInt(b) * BigInt(b)) % BigInt(MOD));
        exp = Math.floor(exp / 2);
    }
    return Number((BigInt(baseCount) * BigInt(pow2k)) % BigInt(MOD));
}

The base case is the plain number: a number blueprint has exactly one valid skyline, so we return 1. For an object, we recursively compute the count for its base, then multiply by 2^k where k is the number of steps.

The power of two is computed with fast exponentiation by squaring, which lets us raise 2 to a large exponent quickly. We use BigInt for the modular multiplication so the intermediate values never overflow, and take the result modulo MOD at each step.

Let us trace the example blueprint = {"base": {"base": 5, "steps": [1]}, "steps": [2, 3]}. The inner base {"base": 5, "steps": [1]} has a number base (count 1) and one step, so it counts as 1 * 2^1 = 2. The outer blueprint then multiplies that by 2^2 = 4, giving 2 * 4 = 8, which matches the expected answer.

The time complexity is O(depth) for the recursion plus O(log k) for each fast-power call, and the space complexity is O(depth) from the recursive call stack.

Best Answers

java
import java.util.*;

class Solution {
    private static final int MOD = 1000000007;

    public int count_valid_skylines(Object blueprint) {
        if (blueprint instanceof Number) {
            return 1;
        }
        Map<String, Object> fork = (Map<String, Object>) blueprint;
        long baseCount = count_valid_skylines(fork.get("base"));
        List<?> steps = (List<?>) fork.get("steps");
        int k = (steps != null) ? steps.size() : 0;
        
        long pow2k = 1;
        long b = 2;
        while (k > 0) {
            if (k % 2 == 1) pow2k = (pow2k * b) % MOD;
            b = (b * b) % MOD;
            k /= 2;
        }
        
        return (int) ((baseCount * pow2k) % MOD);
    }
}