To solve this problem, we'll implement a greedy algorithm that tries to balance the total weights of the buses. Given that the students are in a fixed order and buses can only pick up students from the front of the queue, we need to decide the bus capacities (12 or 18) at each step to minimize the difference between the heaviest and lightest buses.
Here is the complete Java solution:
public class Solution {
public int findLowestWeightDifference(int n, int[] weights) {
// Input validation
if (n < 1 || n > 200000) {
throw new IllegalArgumentException("n is not in the valid range [1, 2*10^5]");
}
if (weights == null || weights.length == 0) {
throw new IllegalArgumentException("The weights array is empty");
}
if (weights.length != n) {
throw new IllegalArgumentException("The length of weights array does not match n");
}
for (int weight : weights) {
if (weight < 1 || weight > 200) {
throw new IllegalArgumentException("Weights are not in the valid range [1, 200]");
}
}
if (n % 6 != 0) {
throw new IllegalArgumentException("Total number of students n must be divisible by 6");
}
if (n < 12) {
throw new IllegalArgumentException("Total number of students n is less than the minimum bus capacity 12");
}
// Compute total weight and average weight per student
long totalWeight = 0;
for (int weight : weights) {
totalWeight += weight;
}
double averageWeightPerStudent = (double) totalWeight / n;
// Precompute prefix sums of weights
long[] prefixSums = new long[n + 1];
for (int i = 0; i < n; i++) {
prefixSums[i + 1] = prefixSums[i] + weights[i];
}
int i = 0;
long maxBusWeight = 0;
long minBusWeight = Long.MAX_VALUE;
while (i < n) {
int remainingStudents = n - i;
double averageWeightPerUnit = averageWeightPerStudent;
long sum12 = -1;
long sum18 = -1;
double deviation12 = Double.MAX_VALUE;
double deviation18 = Double.MAX_VALUE;
if (remainingStudents >= 12) {
sum12 = prefixSums[i + 12] - prefixSums[i];
deviation12 = Math.abs((double) sum12 / 12 - averageWeightPerUnit);
}
if (remainingStudents >= 18) {
sum18 = prefixSums[i + 18] - prefixSums[i];
deviation18 = Math.abs((double) sum18 / 18 - averageWeightPerUnit);
}
if (remainingStudents == 12) {
// Must choose capacity 12
maxBusWeight = Math.max(maxBusWeight, sum12);
minBusWeight = Math.min(minBusWeight, sum12);
i += 12;
} else if (remainingStudents == 18) {
// Can choose capacity 12 or 18, pick the better one
if (deviation12 <= deviation18) {
maxBusWeight = Math.max(maxBusWeight, sum12);
minBusWeight = Math.min(minBusWeight, sum12);
i += 12;
} else {
maxBusWeight = Math.max(maxBusWeight, sum18);
minBusWeight = Math.min(minBusWeight, sum18);
i += 18;
}
} else if (remainingStudents < 12) {
// Cannot proceed, not enough students for minimum bus capacity
throw new IllegalArgumentException("Cannot allocate remaining students to buses");
} else {
// Choose the option with minimal deviation
if (deviation12 <= deviation18) {
maxBusWeight = Math.max(maxBusWeight, sum12);
minBusWeight = Math.min(minBusWeight, sum12);
i += 12;
} else {
maxBusWeight = Math.max(maxBusWeight, sum18);
minBusWeight = Math.min(minBusWeight, sum18);
i += 18;
}
}
}
return (int) (maxBusWeight - minBusWeight);
}
}
Explanation and Error Handling:
-
Input Validation:
- Checks if
n is within [1, 200000].
- Checks if the
weights array is not empty and matches the length n.
- Ensures all weights are within
[1, 200].
- Checks if
n is divisible by 6 since bus capacities are multiples of 6 (12 and 18). If not, throws an error as it's impossible to allocate all students to buses without breaking the rules.
- Throws an error if
n is less than 12, as the minimum bus capacity is 12.
-
Algorithm:
- Computes the total weight and average weight per student.
- Uses prefix sums to quickly calculate the sum of weights for the next 12 or 18 students.
- At each step, it decides whether to use a bus of capacity 12 or 18 by comparing the deviation from the average weight per capacity unit.
- Updates the
maxBusWeight and minBusWeight after each bus is allocated.
- When only 12 students are remaining, it must use a bus of capacity 12.
- If less than 12 students are remaining at any point, it throws an error as it cannot allocate buses accordingly.
-
Edge Cases:
- If the remaining number of students is exactly 12 or 18, it chooses the bus capacity accordingly.
- If the remaining students are less than 12, it throws an error since buses cannot carry fewer than 12 students.
-
Return Value:
- Returns the minimal possible difference between the heaviest and lightest buses after allocation.
This solution ensures that the buses are allocated in a way that balances their total weights as much as possible, given the constraints of bus capacities and the fixed order of students. The error handling ensures that invalid inputs or impossible scenarios are properly reported.