#3492
Easy Algorithms Maximum containers on a ship
Math
75.2% acceptance
Feb 25, 2026
64
13
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.
Solution
Rust
Time O(1)
Space O(1)
impl Solution {
pub fn max_containers(n: i32, w: i32, max_weight: i32) -> i32 {
let capacity = (n as i64 * n as i64).min(max_weight as i64 / w as i64) as i32;
capacity
}
}