BudiBadu Logo

Lantern Stall Picks

Array Easy 1 views
Like2

Festival guests rate lantern stalls from 0 to n, but the operations team only wants to focus on the top contenders. In Lantern Stall Picks, your task is to find the K-th Largest rating in a jumbled list of scores. This helps the organizers decide which stall receives the coveted "Best Glow" award for the night!

The "secret sauce" here is Sorting and Indexing. While you could use complex selection algorithms, the most straightforward way is to sort the list in descending order and pick the value at index k-1. This approach is fast (O(n log n)) and incredibly reliable for any rating range. You must handle duplicate scores (stalls with matching popularity) with care, ensuring every entry is counted. If the list is empty or the rank is out of bounds, return standard safety values. This ensures the awards ceremony stays fair and transparent for all the festival's talented vendors!

If sorting is part of the strategy, do it intentionally as a preprocessing step to simplify downstream logic such as merging, ordering, or comparison. After sorting, keep output semantics precise: preserve expected structure, avoid dropping valid entries, and ensure tied cases still follow deterministic order rules.

Examples

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 - Budibadu
Recommendation Algorithm Flow for Lantern Stall Picks - Budibadu

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;
    }
}