Maximum Containers on a Ship - Problem

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.

Input & Output

Example 1 — Weight Limited
$ Input: n = 3, w = 10, maxWeight = 50
Output: 5
💡 Note: Deck can hold 3×3 = 9 containers, but weight limit allows only 50÷10 = 5 containers. Take minimum: 5.
Example 2 — Deck Limited
$ Input: n = 2, w = 5, maxWeight = 100
Output: 4
💡 Note: Deck can hold 2×2 = 4 containers, weight limit allows 100÷5 = 20 containers. Take minimum: 4.
Example 3 — Equal Constraints
$ Input: n = 4, w = 8, maxWeight = 128
Output: 16
💡 Note: Deck holds 4×4 = 16 containers, weight allows 128÷8 = 16 containers. Both equal, so 16.

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ w ≤ 104
  • 1 ≤ maxWeight ≤ 109

Visualization

Tap to expand
Maximum Containers Problem: Two Constraints3×3 Deck (9 spots)Deck Limit9 containersWeight Limit50÷10 = 5Result: 5min(9, 5) = 5Maximum containers that can fit = 5
Asked in
Amazon 25 Google 20 Microsoft 15
23.4K Views
Medium Frequency
~5 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen