Skip to main content

Maximum Containers on a Ship - Solution & Explanation

EasyMath5 min readAsked at: Google
Practice this problem

Problem Statement

You are given a positive integer n representing an n x n cargo deck on a ship. Each cell on the deck can hold one container with a weight of exactly w.

However, the total weight of all containers, if loaded onto the deck, must not exceed the ship's maximum weight capacity, maxWeight.

Return the maximum number of containers that can be loaded onto the ship.

 

Example 1:

Input: n = 2, w = 3, maxWeight = 15

Output: 4

Explanation:

The deck has 4 cells, and each container weighs 3. The total weight of loading all containers is 12, which does not exceed maxWeight.

Example 2:

Input: n = 3, w = 5, maxWeight = 20

Output: 4

Explanation:

The deck has 9 cells, and each container weighs 5. The maximum number of containers that can be loaded without exceeding maxWeight is 4.

 

Constraints:

  • 1 <= n <= 1000
  • 1 <= w <= 1000
  • 1 <= maxWeight <= 109

Approach Overview

Problem Overview: You are given the number of container slots available on a ship and the ship's total weight capacity. Each container has the same weight. The task is to compute the maximum number of containers that can be loaded without exceeding either the slot limit or the total weight capacity.

Approach 1: Direct Mathematics (O(1) time, O(1) space)

This problem reduces to a simple constraint comparison. The ship can only hold a limited number of containers because of two independent constraints: the number of available slots on the deck and the maximum weight capacity. First compute the total slot capacity (for example rows * cols if the ship deck is modeled as a grid). Then compute how many containers the ship can support based on weight using maxWeight / containerWeight. The final answer is the smaller of these two values because both constraints must be satisfied.

The key insight is that no simulation or iteration is required. Both constraints are independent and can be evaluated with basic arithmetic. Once you calculate the slot limit and the weight-based limit, you simply return min(slotCapacity, weightLimitContainers). This is why the solution belongs to the Math category rather than requiring any complex data structure or algorithm.

This constant-time computation is optimal. Regardless of input size, the algorithm performs only a few arithmetic operations and a single comparison. The time complexity remains O(1) and the space complexity is also O(1) since no additional memory structures are used.

Problems like this commonly appear in interview rounds to test whether you recognize when a problem can be simplified into a direct mathematical formula rather than writing unnecessary loops or simulations. Recognizing these patterns is a useful skill in mathematical reasoning and basic constraint-based optimization.

Recommended for interviews: The mathematical formula approach is exactly what interviewers expect. A brute-force simulation of filling containers would technically work but would introduce unnecessary iteration. Demonstrating that you can reduce the problem to min(totalSlots, maxWeight / containerWeight) shows strong problem decomposition and mathematical reasoning.

Solution

First, we calculate the maximum weight the boat can carry, which is n times n times w. Then, we take the minimum of this value and maxWeight, and divide it by w.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force SimulationO(n)O(1)Conceptual approach where containers are added one by one until a constraint breaks
Direct MathematicsO(1)O(1)Best approach when both slot capacity and weight capacity can be computed directly

Video Solution

3492. Maximum Containers on a Ship (Leetcode Easy) • Programming Live with Larry • 227 views views

Watch 8 more video solutions →

Frequently Asked Questions

Is Maximum Containers on a Ship easy or hard?
Maximum Containers on a Ship is categorized as an Easy problem. The main challenge is recognizing that the constraints reduce to a simple mathematical formula rather than implementing a loop or simulation.
Maximum Containers on a Ship Python/Java solution
The implementation is only a few lines in Python, Java, C++, Go, or TypeScript. Calculate the deck capacity, compute the number of containers allowed by weight, and return the minimum of the two values.
How to solve Maximum Containers on a Ship in O(1)?
First compute the total container slots on the ship, typically rows multiplied by columns. Then compute how many containers can be carried based on the ship's weight limit using maxWeight divided by containerWeight. The final answer is min(totalSlots, weightLimitContainers).
What is the best approach for Maximum Containers on a Ship?
The best approach is a direct mathematical calculation. Compute the total number of available container slots and the number of containers allowed by the ship's weight capacity. The answer is the minimum of these two values. This runs in O(1) time and O(1) space.
Is Maximum Containers on a Ship asked at Google/Amazon/Meta?
This problem represents the type of easy math and constraint reasoning questions that appear in early interview rounds or online assessments. Similar arithmetic optimization problems have appeared in interviews at large tech companies and coding platforms.
What data structure is used in Maximum Containers on a Ship?
No complex data structure is required. The problem is solved using basic arithmetic operations and a minimum comparison, making it a pure math-based problem.
What is the time complexity of Maximum Containers on a Ship?
The optimal solution runs in O(1) time because it only performs a few arithmetic operations and one minimum comparison. No loops or additional data structures are required, so the space complexity is also O(1).

Ready to solve this problem?

Practice Maximum Containers on a Ship with our built-in code editor and test cases.

Practice on FleetCode