Input: n =2, w =3, maxWeight =15Output: 4Explanation:
The deck has 4 cells, and each container weighs 3. The total weight of loading
all containers is12, which does not exceed `maxWeight`.
Input: n =3, w =5, maxWeight =20Output: 4Explanation:
The deck has 9 cells, and each container weighs 5. The maximum number of
containers that can be loaded without exceeding `maxWeight`is4.
The problem is to maximize the number of containers (each of weight w) that can be loaded onto an n x n deck, without exceeding the ship’s weight limit maxWeight. The answer is simply the minimum of the total number of cells (n * n) and the maximum number of containers that fit within the weight limit (maxWeight // w).
classSolution {
public:int maxContainers(int n, int w, int maxWeight) {
int total = n * n;
int byWeight = maxWeight / w;
returnmin(total, byWeight);
}
};
classSolution {
publicintmaxContainers(int n, int w, int maxWeight) {
int total = n * n;
int byWeight = maxWeight / w;
return Math.min(total, byWeight);
}
}
1
2
3
4
5
6
7
classSolution {
funmaxContainers(n: Int, w: Int, maxWeight: Int): Int {
val total = n * n
val byWeight = maxWeight / w
return minOf(total, byWeight)
}
}
1
2
3
4
5
classSolution:
defmaxContainers(self, n: int, w: int, maxWeight: int) -> int:
total = n * n
by_weight = maxWeight // w
return min(total, by_weight)
1
2
3
4
5
6
7
impl Solution {
pubfnmax_containers(n: i32, w: i32, max_weight: i32) -> i32 {
let total = n * n;
let by_weight = max_weight / w;
total.min(by_weight)
}
}