Emberstone Step Glow
This problem feels like a little puzzle you can solve one step at a time. In Emberstone Step Glow, you are trying to work toward the right number by following one clear idea.
Illuminate emberstone step markers recursively 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 terraces = 2, the answer is 21. The second terrace adds one ember and mirrors the earlier route four times. Another example is terraces = 5, which gives 1365. Five terraces maintain the rule, revealing a glowing path of 1365 embers.
This 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
The second terrace adds one ember and mirrors the earlier route four times.
Five terraces maintain the rule, revealing a glowing path of 1365 embers.
Only the first emberstone glows.
Algorithm Flow
Solution Approach
This problem follows a rapid growth pattern where each new terrace adds one ember and mirrors the earlier route four times. The total at each step is built from the previous one, which we can capture with a geometric-series formula.
The example values 1, 5, 21, 85, 341 for terraces 0 through 4 correspond to the recurrence f(n) = 4 * f(n - 1) + 1. This is the sum of a geometric series with ratio 4, whose closed form is (4^(n + 1) - 1) / 3.
Here is the implementation:
The formula computes 4^0 + 4^1 + ... + 4^n. Using the geometric-series identity, this equals (4^(n + 1) - 1) / (4 - 1), which simplifies to (4^(n + 1) - 1) / 3.
Let us verify with n = 2. The formula gives (4^3 - 1) / 3 = (64 - 1) / 3 = 21, matching the expected answer. For n = 0, we get (4 - 1) / 3 = 1, the single initial ember.
Because it is a single expression, the time complexity is O(1) and the space complexity is O(1).
Best Answers
class Solution {
public int emberstone_step_glow(int n) {
int result = 1;
for (int i = 0; i < n; i++) {
result = result * 4 + 1;
}
return result;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
