JuniorMath

Maximum Containers on a Ship

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
Inputn = 2, w = 3, maxWeight = 15
Output4
The deck has 4 cells, and each container weighs 3; loading all containers weighs 12, which does not exceed maxWeight.
Example 2
Inputn = 3, w = 5, maxWeight = 20
Output4
The deck has 9 cells, and each container weighs 5, so at most 4 containers can be loaded without exceeding maxWeight.

Constraints

  • 1 <= n <= 1000
  • 1 <= w <= 1000
  • 1 <= maxWeight <= 10^9

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate