#3861

Minimum Capacity Box

Easy
ArrayArrayLinear Search
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n)
O(n)
Space
O(1)
O(1)
💡

Intuition

Time O(n)Space O(1)

We efficiently find the minimum box that can hold the item by scanning through the list once, keeping track of the best candidate.

⚙️

Algorithm

3 steps
  1. 1Step 1: Initialize min_index to -1.
  2. 2Step 2: Iterate through each box in capacity.
  3. 3Step 3: If capacity[i] >= itemSize and (min_index == -1 or capacity[i] < capacity[min_index]), update min_index.
solution.py7 lines
1def min_capacity_box(capacity, itemSize):
2    min_index = -1
3    for i in range(len(capacity)):
4        if capacity[i] >= itemSize:
5            if min_index == -1 or capacity[i] < capacity[min_index]:
6                min_index = i
7    return min_index

Complexity note: We only traverse the array once, leading to O(n) time complexity with constant space usage.

  • 1Iterate through the array to find the minimum capacity.
  • 2Keep track of the smallest index for ties.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.