Reputation: 21
public static int maxIceCream(int[][] costs, int coins) {
Arrays.sort(costs);
boolean found = false;
for (int i = 0; i < costs.length; ++i) {
if (coins != costs[i]) {
coins -= costs[i];
found = true;
break;
} else {
return i;
}
}
return costs.length;
}
compare to integer and integer array
Upvotes: 0
Views: 132
Reputation: 13
You are getting the error message because "costs" is a 2D matrix and "coins" is an integer. So, you can't compare an integer(int) to array of integer(int[]). Try looping two times over the "costs" to compare to all the values
public static int maxIceCream(int[][] costs, int coins) {
Arrays.sort(costs);
boolean found = false;
for (int i = 0; i < costs.length; ++i) {
for (int j = 0; j < costs[i].length; ++j) {
if (coins != costs[i][j]) {
coins -= costs[i][j];
found = true;
break;
} else {
return i;
}
}
}
return costs.length;
}
Upvotes: 1