3492. Maximum Containers on a Ship LeetCode Solution
In this guide, you will get 3492. Maximum Containers on a Ship LeetCode Solution with the best time and space complexity. The solution to Maximum Containers on a Ship problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Containers on a Ship solution in C++
- Maximum Containers on a Ship solution in Java
- Maximum Containers on a Ship solution in Python
- Additional Resources
Problem Statement of 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:
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 <= 1000
1 <= w <= 1000
1 <= maxWeight <= 109
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
3492. Maximum Containers on a Ship LeetCode Solution in C++
class Solution {
public:
int maxContainers(int n, int w, int maxWeight) {
return min(n * n, maxWeight / w);
}
};
/* code provided by PROGIEZ */
3492. Maximum Containers on a Ship LeetCode Solution in Java
class Solution {
public int maxContainers(int n, int w, int maxWeight) {
return Math.min(n * n, maxWeight / w);
}
}
// code provided by PROGIEZ
3492. Maximum Containers on a Ship LeetCode Solution in Python
class Solution:
def maxContainers(self, n: int, w: int, maxWeight: int) -> int:
return min(n * n, maxWeight // w)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.