There are two kinds of cups, the capacities are v1 and v2 respectively, and there are p balls in total, how to fill them to minimize the number of cups. if no answer, return -1;
eg:
v1 = 3, v2 = 4, p = 10
output: 3
explanation: two v1, one v2
eg:
v1 = 3, v2 = 4, p = 6
output: 2
explanation: two v1
If you have some edge cases, feel free to share with me.
my solution:
class MinimumNumberOfCups {
public static void main(String[] args) {
int[][] test = new int[][]{{3,4,6},{3,4,10},{4,6,7}};
for (int[] x : test) {
int ans = calc(x[0],x[1],x[2]);
System.out.println(ans);
}
}
public static int calc(int v1, int v2, int p) {
int r1 = p / v1;
int res = 100010;
for (int i = 0; i <= r1; i++) {
if ((p - i * v1) % v2 == 0) {
res = Math.min(res, i + ((p - i * v1) / v2));
}
}
return res == 100010 ? -1 : res;
}
}