#3861
Easy Algorithms Minimum capacity box
Array
72.0% acceptance
Mar 15, 2026
44
1
You are given an integer array capacity, where capacity[i] represents the capacity of the ith box, and an integer itemSize representing the size of an item.
The ith box can store the item if capacity[i] >= itemSize.
Return an integer denoting the index of the box with the minimum capacity that can store the item. If multiple such boxes exist, return the smallest index.
If no box can store the item, return -1.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn minimum_index(capacity: Vec<i32>, item_size: i32) -> i32 {
let mut best_idx: i32 = -1;
let mut best_cap = i32::MAX;
for (i, &cap) in capacity.iter().enumerate() {
if cap >= item_size && cap < best_cap {
best_cap = cap;
best_idx = i as i32;
}
}
best_idx
}
}