[LeetCode] Minimum Capacity Box

3861. Minimum Capacity Box

You are given an integer array capacity, where capacity[i] represents the capacity of the i^th box, and an integer itemSize representing the size of an item.

The i^th 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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int minimumIndex(vector<int>& capacity, int itemSize) {
map<int,int> at;
for(int i = capacity.size() - 1; i >= 0; i--) {
if(capacity[i] >= itemSize) at[capacity[i]] = i;
}
if(at.size() == 0) return -1;
return begin(at)->second;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/minimum-capacity-box/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.