水壶问题
题目
有两个水壶,容量分别为 jug1Capacity 和 jug2Capacity 升。水的供应是无限的。确定是否有可能使用这两个壶准确得到 targetCapacity 升。
如果可以得到 targetCapacity 升水,最后请用以上水壶中的一或两个来盛放取得的 targetCapacity 升水。
你可以:
装满任意一个水壶
清空任意一个水壶
从一个水壶向另外一个水壶倒水,直到装满或者倒空
示例 1:
输入: jug1Capacity = 3, jug2Capacity = 5, targetCapacity = 4
输出: true
解释:来自著名的 "Die Hard"
示例 2:
输入: jug1Capacity = 2, jug2Capacity = 6, targetCapacity = 5
输出: false
示例 3:
输入: jug1Capacity = 1, jug2Capacity = 2, targetCapacity = 3
输出: true
提示:
1 <= jug1Capacity, jug2Capacity, targetCapacity <= 106
方法一 求最大公约数
为了方便说明,用C1代表小杯子,C2代表大杯子。
分析每次倒水加水的各种情况
1、倒掉C1或者C2的水。
2、给空的C1或者C2加满水。
3、C2向C1或C1向C2中倒水,等价于总的含水量没变化。
4、给不满的C1或者C2加满水,此时可以理解成为先把C1或者C2倒完水,然后加满水。
总结,每次倒水,加水都可以总结为总量不变、加C1、加C2的情况。
由此可以得出
a
x
+
b
x
=
z
ax + bx = z
ax+bx=z的公式,也就是说z是a、b的最小公约数的倍数即可。
class Solution {
public boolean canMeasureWater(int x, int y, int z) {
if(x + y < z){
return false;
}
return z % gcd(x,y) == 0;
}
public int gcd(int x, int y){
int remain = x % y;
while(remain != 0){
x = y;
y = remain;
remain = x % y;
}
return y;
}
}
方法二
第二种方案是模拟真实的倒/加水过程,按照深度搜索进行遍历,需要记录每次搜索过程中的倒/加水结果,用TreeSet对倒/加水结果进行维护,避免重复。
倒/加水分为以下几种情况:
1、给C1加满水
2、给C2加满水
3、倒掉C1的所有水
4、倒掉C2的所有水
5、用C1往C2加水
6、用C2往C1加水
class Solution {
public boolean canMeasureWater(int c1, int c2, int c3) {
if(c1 + c2 < c3){
return false;
}
Deque<int[]> s = new LinkedList();
//从第一个开始
s.push(new int[]{0,0});
Set<Long> set = new HashSet();
//DFS
while(!s.isEmpty()){
//弹出栈顶数据
//如果包含,则直接继续循环
//否则继续运行
if(set.contains(HashNum(s.peek()))){
s.pop();
continue;
}
set.add(HashNum(s.peek()));
int[] res = s.pop();
int n1 = res[0], n2 = res[1];
if(n1 + n2 == c3 || n1 == c3 || n2== c3){
return true;
}
//1、给C1加满水
s.push(new int[]{c1,n2});
//2、给C2加满水
s.push(new int[]{n1,c2});
//3、倒掉C1的所有水
s.push(new int[]{0, n2});
//4、倒掉C2的所有水
s.push(new int[]{n1, 0});
int sub1 = Math.min(c2- n2, n1);
//5、用C1往C2加水
s.push(new int[]{n1 - sub1, n2 + sub1});
int sub2 = Math.min(c1- n1, n2);
//6、用C2往C1加水
s.push(new int[]{n1 + sub2, n2 - sub2});
}
return false;
}
public long HashNum(int [] num){
return(long) num[0] * 1000001 + (num[1]);
}
}