Which trucks are overloaded
A dispatch list pairs each truck with its load and its capacity. Count how many trucks are carrying more than their capacity.
- The two lists are the same length and read like parallel records.
- A load exactly equal to capacity is fine.
overCapacity(loads: list<int>, capacity: list<int>) → int
Java needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
int overCapacity(List<Integer> loads, List<Integer> capacity) {
}
Worked examples
| Call | Result |
|---|---|
overCapacity(Main.<Integer>ls(10, 20, 30), Main.<Integer>ls(8, 20, 40)) | 1 |
overCapacity(Main.<Integer>ls(1, 2), Main.<Integer>ls(9, 9)) | 0 |
overCapacity(Main.<Integer>ls(10, 10), Main.<Integer>ls(4, 4)) | 2 |
overCapacity(Main.<Integer>ls(), Main.<Integer>ls()) | 0 |
Hint
Iterate the indices and compare.
Reference solution in Java
int overCapacity(List<Integer> loads, List<Integer> capacity) {
int n = 0;
for (int i = 0; i < loads.size(); i++) if (loads.get(i) > capacity.get(i)) n++;
return n;
}