Code Logo

Lantern Stage Volume Check

Published at05 Jan 2026
Easy 10 views
Like22

This problem gives you a sorted list of stage volumes and a list of audience requests. For each request, you need to find the first stage whose volume is at least that request.

The answer for one request is the stage position, using 1-based indexing. If no stage is loud enough, return -1 for that request.

For example, if volumes = [4,4,10] and requests = [4,5], the answer is [1,3]. The first request is already satisfied by stage 1, while the first stage that reaches 5 is stage 3 with volume 10. If volumes = [] and requests = [3], the answer is [-1] because there is no stage to choose from.

So for each request, return the earliest stage index whose volume is large enough, or -1 if it never happens.

Example Input & Output

Example 1
Input
volumes = [], requests = [3]
Output
[-1]
Explanation

Without data, no request can be satisfied.

Example 2
Input
volumes = [4,4,10], requests = [4,5]
Output
[1,3]
Explanation

The first stage already has 4; stage 3 is the first to meet 5.

Example 3
Input
volumes = [5,9,15,20], requests = [1,15,25]
Output
[1,3,-1]
Explanation

Stage 1 meets 1, stage 3 reaches 15, no stage hits 25.

Algorithm Flow

Recommendation Algorithm Flow for Lantern Stage Volume Check
Recommendation Algorithm Flow for Lantern Stage Volume Check

Solution Approach

The clean way to solve this is with a lower bound binary search for each request. Since volumes is sorted, we can quickly find the first position where the value is greater than or equal to the requested volume.

For one request, the binary search looks like this:

let left = 0;
let right = volumes.length;

while (left < right) {
  const mid = Math.floor((left + right) / 2);
  if (volumes[mid] >= request) {
    right = mid;
  } else {
    left = mid + 1;
  }
}

After the loop, left is the first index where the stage volume is big enough. If left is still inside the array, the answer is left + 1 because the problem uses 1-based stage numbers. If left === volumes.length, then no stage satisfies that request and the answer is -1.

Run this search for every request and collect the results in order. If there are n stages and m requests, the total time is O(m log n), with O(1) extra space besides the output list.

Best Answers

java
import java.util.*;
class Solution {
    public Object lantern_stage_volume_check(Object input) {
        Object[] args = (Object[]) input;
        int[] volumes = (int[]) args[0];
        int[] requests = (int[]) args[1];
        int[] res = new int[requests.length];
        for (int i = 0; i < requests.length; i++) {
            int low = 0, high = volumes.length - 1, ans = -1;
            while (low <= high) {
                int mid = low + (high - low) / 2;
                if (volumes[mid] >= requests[i]) { ans = mid + 1; high = mid - 1; }
                else low = mid + 1;
            }
            res[i] = ans;
        }
        return res;
    }
}