Starlight Lantern Chain
This challenge becomes much easier once you know exactly what to keep, change, or count. In Starlight Lantern Chain, you are trying to work toward the right number by following one clear idea.
Calculate starlight lantern chain length 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 tiers = 2, the answer is 7. The second tier adds a lantern and doubles the earlier glow. Another example is tiers = 0, which gives 1. Only the initial lantern shines.
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 tier adds a lantern and doubles the earlier glow.
Only the initial lantern shines.
Three tiers preserve the pattern, revealing fifteen lanterns in total.
Algorithm Flow
Solution Approach
This problem follows a doubling growth pattern where each lantern links onto the previous chain, effectively doubling the structure each step. The totals grow by powers of two, which lets us solve it with a simple formula.
The example values 1, 3, 7, 15, 31, 63 for inputs 0 through 5 match the pattern 2^(n + 1) - 1, which is the sum of the geometric series 1 + 2 + 4 + ... + 2^n.
Here is the implementation:
The formula computes 2^(n + 1) - 1. Because the sum 1 + 2 + 4 + ... + 2^n equals 2^(n + 1) - 1, this gives the total number of lanterns directly.
Let us verify with n = 2. The formula gives 2^3 - 1 = 7, matching the expected answer. For n = 5, we get 2^6 - 1 = 63. And for n = 0, we get 2 - 1 = 1, the single starting lantern.
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 find_longest_increasing_chain(int n) {
return (1 << (n + 1)) - 1;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
