Watch 3 video solutions for Maximum Containers on a Ship, a easy level problem involving Math. This walkthrough by Programming Live with Larry has 216 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 10001 <= w <= 10001 <= maxWeight <= 109Problem 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.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Simulation | O(n) | O(1) | Conceptual approach where containers are added one by one until a constraint breaks |
| Direct Mathematics | O(1) | O(1) | Best approach when both slot capacity and weight capacity can be computed directly |