Lantern Stage Volume Check
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
Without data, no request can be satisfied.
The first stage already has 4; stage 3 is the first to meet 5.
Stage 1 meets 1, stage 3 reaches 15, no stage hits 25.
Algorithm Flow

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:
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
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;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
