- 2

Looking for the solution in java

Write a static method minTrucks() that accepts as parameters, total load and an array of available truck sizes and returns the minimum number of trucks required to transport the exact load. If it is not possible to transport the load, your method should return -1. f.e. minTrucks(100, {40, 50, 10, 5}) return 2 miniTrucks(9, {10, 30, 40, 5}) return -1 miniTrucks(17, {3, 20, 50}) return -1 I tried the blow, but I can not fulfill some situations. public static int minTrucks(int Load, int[] truckSizes){ int minTrucks = 0; int totLoad = Load; Arrays.sort(truckSizes); for(int i = truckSizes.length - 1; i >= 0; i--){ if(totLoad % truckSizes[i] == 0){ minTrucks += totLoad / truckSizes[i]; return minTrucks; }else if(Load % truckSizes[i] == 0){ minTrucks = Load / truckSizes[i]; return minTrucks; }else{ minTrucks += totLoad / truckSizes[i]; totLoad %= truckSizes[i]; } } return -1; }

22nd Jan 2018, 5:59 PM
cheng zhang
cheng zhang - avatar
2 Respostas
+ 18
yes , its homework only (with no try or code) //i unfollow those threads //👉edit ::: its edited now so here is edited answer 👍 1)arrange second array within that 2-d array in descending order 2)then run a loop until arr[1][i] <= arr [0][0] & see how many time loop runs & also see is arr [0][0] is achieved or not ,then return number else -1
22nd Jan 2018, 5:32 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
0
sounds like homework
22nd Jan 2018, 5:28 PM
Jeremy
Jeremy - avatar