Answer the question
In order to leave comments, you need to log in
Java: Why doesn't a while loop with a nested for loop and an if condition work correctly?
I am solving a problem for a queue of an arbitrary number of customers at *n* checkouts in a supermarket, where the queue is given by an array of integers, each of which means the service time of the corresponding customer at the checkout.
There was a problem in the most difficult case, when the number of cash desks is more than one (n > 1) and the number of customers in the queue is greater than the number of cash desks (customers.length > n).
import java.util.Arrays;
public class Solution {
public static int solveSuperMarketQueue(int[] customers, int n) {
int sum = 0;
int[] nCustomers;
int[] restOfCustomers;
if (customers.length == 0) {
return 0;
} else if (customers.length == 1) {
return customers[0];
} else {
if (n == 0) {
return 0;
} else if (n == 1) {
sum = sumOfArray(customers);
} else if (customers.length <= n) {
int customerMax = customers[0];
for (int i = 0; i < n; i++) {
if (customers[i] > customerMax) {
customerMax = customers[i];
}
}
return customerMax;
} else {
nCustomers = Arrays.copyOfRange(customers, 0, n);
restOfCustomers = Arrays.copyOfRange(customers, n, customers.length);
int serviceTime = 0;
while (sumOfArray(nCustomers) > 0) {
for (int i = 0; i < nCustomers.length; i++) {
nCustomers[i]--;
if (nCustomers[i] == 0) {
nCustomers[i] = restOfCustomers[0];
restOfCustomers = Arrays.copyOfRange(restOfCustomers, 1, restOfCustomers.length);
}
serviceTime++;
}
return serviceTime;
}
}
}
return sum;
}
public static int sumOfArray(int ary[]) {
int s = 0;
for (int i = 0; i < ary.length; i++) {
s += ary[i];
}
return s;
}
}
} else {
nCustomers = Arrays.copyOfRange(customers, 0, n);
restOfCustomers = Arrays.copyOfRange(customers, n, customers.length);
int serviceTime = 0;
while (sumOfArray(nCustomers) > 0) {
for (int i = 0; i < nCustomers.length; i++) {
nCustomers[i]--;
if (nCustomers[i] == 0) {
nCustomers[i] = restOfCustomers[0];
restOfCustomers = Arrays.copyOfRange(restOfCustomers, 1, restOfCustomers.length);
}
serviceTime++;
}
return serviceTime;
}
Answer the question
In order to leave comments, you need to log in
you are doing the wrong things. that is, both the implementation is weak, and the logic.
by implementation, for example, why copy the contents of an array with a queue at all, when you can just store the current position? it only added quadratic computational complexity in vain.
and logically, you don’t need to make a step-by-step queue simulator, but distribute people among the cash desks and choose the busiest one.
you can distribute it in one pass in turn: you have a list of cash desks with the current time of release of each cash desk (initially zero), for each person you select the fastest cash desk (which has the minimum time value in the list) and add to it the time of its service. then just choose the largest total time from all cash desks.
it does not even need to handle separately the cases of one cash desk or more cash desks than people, or an empty zero-length queue - it will work quite correctly.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question