🚢 Max Containers on a Cargo Deck 🚢
💡 Intuition:The problem requires us to determine the maximum number of containers that can be loaded onto an n×n cargo deck without exceeding the ship's weight
# 💡 Intuition:
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires us to determine the maximum number of containers that can be loaded onto an n×n cargo deck without exceeding the ship's weight capacity.

Each cell on the deck can hold exactly one container, meaning the maximum possible number of containers is n^2. However, each container has a weight 
w, and we must ensure that their total weight does not exceed maxWeight.

# 🛠️ Approach:
<!-- Describe your approach to solving the problem. -->
1. Brute Force Iteration:

    - Start with 0 containers and iterate from 1 to n^2.

    - Check if adding another container `(total weight = 
w×containers)` exceeds `maxWeight`.

    - If not, update the maximum number of containers.
2. Optimized Approach:

    - Instead of iterating through all values, we can use `min(n*n, maxWeight // w)`
    
    - Since each container weighs w, we can only fit at most `maxWeight // w` containers.
    
    - The total containers must also be ≤ n^2, so the final answer is:
                   `min(n^2, (maxWeight/w))`

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
Brute Force: $$O(n^2)$$ (Iterating over all possible containers)

Optimized: $$O(1)$$ (Direct computation using division and min function)

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
1. Brute force: 
```python3 []
class Solution:
    def maxContainers(self, n: int, w: int, maxWeight: int) -> int:
        res = 0
        for i in range(1, n*n+1):
            if(w*i <= maxWeight):
                res = max(res, i)
        return res
                
```
2. Optimized:
```python3 []
class Solution:
    def maxContainers(self, n: int, w: int, maxWeight: int) -> int:
        return min(n * n, maxWeight // w)

```

![DALL·E 2025-03-23 09.45.16 - A fun and engaging poster asking for upvotes on a LeetCode solution. The poster features a cargo ship with containers labeled as 'Upvotes'. The text o.webp](https://assets.leetcode.com/users/images/9c10a314-7de8-44b0-a6e8-35af390cfb26_1742703333.1395214.webp)

['Greedy', 'Graph', 'Python3']