Celestial Skyline Layout
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 with input: blueprint = 4
Example with input: blueprint = {"base": {"base": 5, "steps": [1]}, "s
Example with input: blueprint = {"base": 4, "steps": [2]}
Algorithm Flow
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:
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
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);
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
