Code Logo

Lantern Stall Picks

Published atDate not available
Easy 0 views
Like0

Think of glowing lantern pieces that need to be handled in the right way. In Lantern Stall Picks, you are trying to work toward the right number by following one clear idea.

Maximize tokens from non-adjacent stalls 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 tokens = [5,1,1,5], the answer is 10. Choose the first and last stalls to avoid adjacency and earn 10 tokens. Another example is tokens = [2,7,9,3,1], which gives 12. Select stalls worth 2, 9, and 1 tokens for a total of 12.

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

Example 1
Input
tokens = [5,1,1,5]
Output
10
Explanation

Choose the first and last stalls to avoid adjacency and earn 10 tokens.

Example 2
Input
tokens = [2,7,9,3,1]
Output
12
Explanation

Select stalls worth 2, 9, and 1 tokens for a total of 12.

Example 3
Input
tokens = []
Output
0
Explanation

With no stalls, the total reward is zero.

Algorithm Flow

Recommendation Algorithm Flow for Lantern Stall Picks
Recommendation Algorithm Flow for Lantern Stall Picks

Best Answers

java
import java.util.*;
class Solution {
    public int calculate_max_items(int[] weights, int capacity) {
        Arrays.sort(weights);
        int count = 0, total = 0;
        for (int w : weights) {
            if (total + w <= capacity) {
                total += w;
                count++;
            } else break;
        }
        return count;
    }
}