Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
2 3 15
3 5 20
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3492: Maximum Containers on a Ship
class Solution {
public int maxContainers(int n, int w, int maxWeight) {
return Math.min(n * n * w, maxWeight) / w;
}
}
// Accepted solution for LeetCode #3492: Maximum Containers on a Ship
func maxContainers(n int, w int, maxWeight int) int {
return min(n*n*w, maxWeight) / w
}
# Accepted solution for LeetCode #3492: Maximum Containers on a Ship
class Solution:
def maxContainers(self, n: int, w: int, maxWeight: int) -> int:
return min(n * n * w, maxWeight) // w
// Accepted solution for LeetCode #3492: Maximum Containers on a Ship
fn max_containers(n: i32, w: i32, max_weight: i32) -> i32 {
let max = n * n * w;
if max_weight <= max {
max_weight / w
} else {
n * n
}
}
fn main() {
let ret = max_containers(2, 3, 15);
println!("ret={ret}");
}
#[test]
fn test() {
assert_eq!(max_containers(2, 3, 15), 4);
assert_eq!(max_containers(3, 5, 20), 4);
assert_eq!(max_containers(2, 3, 4), 1);
}
// Accepted solution for LeetCode #3492: Maximum Containers on a Ship
function maxContainers(n: number, w: number, maxWeight: number): number {
return (Math.min(n * n * w, maxWeight) / w) | 0;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.