[LeetCode] Maximum Containers on a Ship

3492. 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.

c++
1
2
3
4
5
6
7
8
9
class Solution {
public:
long long maxContainers(long long n, int w, long long maxWeight) {
long long res1 = maxWeight / w, res2 = 0;
for(int i = 0; i < n; i++) res2 += n;
return min(res1,res2);
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2025/03/23/PS/LeetCode/maximum-containers-on-a-ship/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.

Related Issues not found

Please contact @SongHayoung to initialize the comment