Skip to main content
Back to problems
Leetcode
Medium
Arrays
Binary Search
Greedy
Amazon
Maximum Containers On A Ship

Determine the maximum number of containers that can be loaded on a ship subject to a capacity limit.

Acceptance 0%
Problem Statement

You are given an array representing the weights or sizes of containers and a ship with a maximum carrying capacity. Choose the largest possible number of containers whose total weight does not exceed the ship's capacity.

Return that maximum count.

This is a typical optimization problem where the answer depends on a feasibility check: given a target number of containers, decide whether it is possible to load that many containers within the capacity limit.

Input Format

  • An integer array containers, where each value is the weight of one container.
  • An integer capacity, the maximum total weight the ship can carry.

The exact input shape may vary by platform, but the core task is to maximize the number of selected containers without exceeding capacity.

Output Format

Return an integer representing the maximum number of containers that can be placed on the ship without the total weight exceeding capacity.

Constraints

  • 1 <= containers.length
  • Container weights are positive integers.
  • capacity is a non-negative integer.
  • The answer must fit in a 32-bit signed integer.
Examples
Sample cases returned by the problem API.

Example 1

Input

containers = [4, 1, 3, 2], capacity = 5

Output

2

Explanation

The best choice is containers with weights 1 and 2, for a total of 3. Any 3-container selection exceeds the capacity.

Example 2

Input

containers = [8, 2, 1, 7], capacity = 10

Output

2

Explanation

Choose containers 1 and 2 for a total of 3, or 1 and 7 for a total of 8. Three containers would exceed the capacity.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.