I am writing an algorithm that should return "yes" if a list of customers are to pay for a movie and receive their change (money) considering the the clerk starts receiving payments with an empty cashier pause, meaning he cant give change from the start. The movie costs 25 $ so if a customer has 50 $ the clerk has to refuse unless he has already received 25 $ from previous customer so is able to use that as change for the next customer.
I have such algorithm
public static String Tickets(int[] peopleInLine) {
int sumOfMoneyWithCashier = 0;
int cost = 25;
for (int i = 0; i < peopleInLine.length; i++) {
if (peopleInLine[i] == cost) {
sumOfMoneyWithCashier += peopleInLine[i];
if (peopleInLine[i + 1] == cost) {
sumOfMoneyWithCashier += cost;
} else if (peopleInLine[i + 1] > cost) {
int change = peopleInLine[i + 1] - cost;
if (sumOfMoneyWithCashier >= change) {
sumOfMoneyWithCashier -= change;
} else {
System.out.println("no");
return "NO";
}
}
} else if (peopleInLine[i] > cost) {
int change = peopleInLine[i] - cost;
if (sumOfMoneyWithCashier >= change) {
sumOfMoneyWithCashier -= change;
} else {
System.out.println("no");
return "NO";
}
}
}
System.out.println("YES");
return "YES";
}
Now it works but not perfect , how can i improve this code so it check handles most of the scenario, ignore case where customer comes with less than 25. customers must come with multiples of 25 between range of {25, 50, 100} How can i make this code better is the question , i would appreciate an example code and explanation basically
Line.Tickets(new int[] {25, 25, 50}) // => YES
Line.Tickets(new int[]{25, 100}) // => NO. Vasya will not have enough money to give change to 100 dollars
Line.Tickets(new int[] {25, 25, 50, 50, 100}) // => NO. Vasya will not have the right bills to give 75 dollars of change (you can't make two bills of 25 from one of 50)
-
How can I improve/make it better is a pretty vague question. If you have a specific situation when the program misbehaves or gives the wrong result than explain that situation (or situations) with example of given input and the result vs the expected result.Joakim Danielson– Joakim Danielson2018年12月12日 13:14:16 +00:00Commented Dec 12, 2018 at 13:14
-
ok i got the task out of a codewars.com so there 12 tests it should pass , currently it passes 10 test , the problem is that codewars,com doesnt show you the tests inputs that were used @JoakimDanielsonvalik– valik2018年12月12日 13:18:59 +00:00Commented Dec 12, 2018 at 13:18
-
(peopleInLine[i + 1] == cost) i am doing this to represent next element in the array @SamzSakerzvalik– valik2018年12月12日 13:19:51 +00:00Commented Dec 12, 2018 at 13:19
-
its just my way of getting next value in an array @SamzSakerzvalik– valik2018年12月12日 13:28:32 +00:00Commented Dec 12, 2018 at 13:28
-
may be not the best way @SamzSakerzvalik– valik2018年12月12日 13:28:57 +00:00Commented Dec 12, 2018 at 13:28
1 Answer 1
This is a simple solution to what you are trying to do see if it works out for you :)
public static String Tickets(int[] peopleInLine) {
int d25 = 0, d50 = 0;
for (int aPeopleInLine : peopleInLine) {
if (aPeopleInLine == 25){
d25++;
}
if (aPeopleInLine == 50) {
d25--;
d50++;
}
if (aPeopleInLine == 100) {
if (d50 > 0) {
d50--;
d25--;
} else {
d25 -= 3;
}
}
if (d25 < 0){
return "NO";
}
}
return "YES";
}
-
yes it works perfectly for all cases , can you tell me the things wrong with code logic ? @SamzSakerzvalik– valik2018年12月12日 13:32:47 +00:00Commented Dec 12, 2018 at 13:32