Code Logo

Lantern Pattern Paths

Published at05 Jan 2026
Array Manipulation Easy 7 views
Like4

Picture a lantern puzzle where each step matters. In Lantern Pattern Paths, you are trying to work toward the right list by following one clear idea.

This problem feels a bit like a maze or map challenge. You need to think about how moves, paths, or links work together. Some places may still connect nicely, while others may be blocked or no longer fit the rule. The answer comes from following those connections carefully.

For example, if the input is base = "Pause by the willow.", markers = [], the answer is ["Pause by the willow."]. Example with input: base = "Pause by the willow.", markers = [] Another example is base = "Listen for distant bells.", markers = ["Golden Spine", "Silver Veil"], which gives ["Enter Golden Spine", "Enter Silver Veil", "Listen for distant bells.", "Mirror Silver Veil", "Listen for distant bells.", "Exit Silver Veil", "Mirror Golden Spine", "Enter Silver Veil", "Listen for distant bells.", "Mirror Silver Veil", "Listen for distant bells.", "Exit Silver Veil", "Exit Golden Spine"]. Example with input: base = "Listen for distant bells.", markers = ["GoThis is a friendly practice problem, but it still rewards careful reading. The key is keeping track of where you can move and which routes still follow the rule.

Example Input & Output

Example 1
Input
base = "Pause by the willow.", markers = []
Output
["Pause by the willow."]
Explanation

Example with input: base = "Pause by the willow.", markers = []

Example 2
Input
base = "Listen for distant bells.", markers = ["Golden Spine", "Silver Veil"]
Output
["Enter Golden Spine", "Enter Silver Veil", "Listen for distant bells.", "Mirror Silver Veil", "Listen for distant bells.", "Exit Silver Veil", "Mirror Golden Spine", "Enter Silver Veil", "Listen for distant bells.", "Mirror Silver Veil", "Listen for distant bells.", "Exit Silver Veil", "Exit Golden Spine"]
Explanation

Example with input: base = "Listen for distant bells.", markers = ["Go

Example 3
Input
base = "Pause by the willow.", markers = ["Azure Bridge"]
Output
["Enter Azure Bridge", "Pause by the willow.", "Mirror Azure Bridge", "Pause by the willow.", "Exit Azure Bridge"]
Explanation

Example with input: base = "Pause by the willow.", markers = ["Azure B

Algorithm Flow

Recommendation Algorithm Flow for Lantern Pattern Paths

Solution Approach

This problem asks us to count the number of unique paths from one corner of a grid to the opposite corner, moving only right and down. For a grid with dimensions a and b, every valid path uses exactly (a - 1) downward moves and (b - 1) rightward moves, regardless of the order.

Because the order of those moves is the only thing that varies, the total number of paths equals the number of ways to choose the positions of the downward moves among all (a + b - 2) moves. That is a binomial coefficient, which we can compute directly.

Here is the implementation:

function lantern_pattern_paths(a, b) {
    const total = a + b - 2;
    const k = Math.min(a - 1, b - 1);
    let result = 1;
    for (let i = 1; i <= k; i++) {
        result = Math.floor(result * (total - k + i) / i);
    }
    return result;
}

The formula is C(a + b - 2, a - 1). We compute it incrementally to avoid large intermediate numbers, using the recurrence that multiplies by the next numerator and divides by the current denominator at each step. Using Math.min for k keeps the loop as short as possible.

Let us verify with a = 3, b = 7. We have total = 8 and k = 2, so the answer is C(8, 2) = 28. For a = 3, b = 2, we get C(3, 2) = 3. For a grid with one row (a = 1), there is exactly one path, so the answer is 1.

The symmetry of the problem is visible in the examples: lantern_pattern_paths(3, 7) and lantern_pattern_paths(7, 3) both equal 28, because the grid is the same when flipped.

The time complexity is O(min(a, b)) due to the loop, and the space complexity is O(1).

Best Answers

java
class Solution {
    public int lantern_pattern_paths(int m, int n) {
        return (int) nCr(m + n - 2, m - 1);
    }
    private long nCr(int n, int r) {
        if (r > n) return 0;
        if (r == 0 || r == n) return 1;
        if (r > n / 2) r = n - r;
        long res = 1;
        for (int i = 1; i <= r; i++) {
            res = res * (n - i + 1) / i;
        }
        return res;
    }
}