↓Skip to main content
  1. LeetCode/

LeetCode 3492: Maximum Containers on a Ship

·1 min· ·
LeetCode Easy Math
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-03-23

  • Submissions: Wrong Answer → Accepted ×2

Approach
#

Take the smaller of two limits:

  • Cells: n × n.
  • Weight: each container weighs w, so at most maxWeight // w (rounded down).

My Attempts
#

Attempt 1 used maxWeight / w, returning a float. For n = 2, w = 3, maxWeight = 7 the answer is 2; it returned 2.333.... Wrapping it in int(...) passed; // is clearer.

Solution
#

class Solution:
    def maxContainers(self, n: int, w: int, maxWeight: int) -> int:
        return min(n * n, maxWeight // w)

Complexity
#

Time O(1), space O(1).

Mistakes to Avoid
#

  • / is float division in Python 3; counts need //.

Interview Explanation
#

The count is limited both by the n² cells and by weight, maxWeight // w, so I take the smaller. Time O(1).

Related

LeetCode 1103: Distribute Candies to People
·1 min
LeetCode Easy Math Simulation
LeetCode 1103 note: simulate the rounds, giving i + 1 candies on the i-th turn, and hand all the rest to the current person when short
LeetCode 1137: N-th Tribonacci Number
·2 mins
LeetCode Easy Math Dynamic-Programming Memoization
LeetCode 1137 note: like Fibonacci, roll three variables; watch the base values T0 = 0, T1 = T2 = 1 and the loop range
LeetCode 1030: Matrix Cells in Distance Order
·2 mins
LeetCode Easy Array Math Geometry Sorting
LeetCode 1030 note: list every coordinate and sort by Manhattan distance to the center; return coordinates, not distances
LeetCode 1025: Divisor Game
·2 mins
LeetCode Easy Math Dynamic-Programming Brainteaser Game-Theory
LeetCode 1025 note: dp[i] holds when some divisor x of i makes dp[i - x] a loss; observing the pattern, the answer is simply whether n is even
LeetCode 989: Add to Array-Form of Integer
·2 mins
LeetCode Easy Array Math
LeetCode 989 note: treat k as the carry, add from the last element backwards, and keep prepending digits while k remains
LeetCode 908: Smallest Range I
·1 min
LeetCode Easy Array Math
LeetCode 908 note: lower the maximum by k and raise the minimum by k; the answer is max(0, max - min - 2k)