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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code