水壶问题
难度:
标签:
题目描述
You are given two jugs with capacities x
liters and y
liters. You have an infinite water supply. Return whether the total amount of water in both jugs may reach target
using the following operations:
- Fill either jug completely with water.
- Completely empty either jug.
- Pour water from one jug into another until the receiving jug is full, or the transferring jug is empty.
Example 1:
Input: x = 3, y = 5, target = 4
Output: true
Explanation:
Follow these steps to reach a total of 4 liters:
- Fill the 5-liter jug (0, 5).
- Pour from the 5-liter jug into the 3-liter jug, leaving 2 liters (3, 2).
- Empty the 3-liter jug (0, 2).
- Transfer the 2 liters from the 5-liter jug to the 3-liter jug (2, 0).
- Fill the 5-liter jug again (2, 5).
- Pour from the 5-liter jug into the 3-liter jug until the 3-liter jug is full. This leaves 4 liters in the 5-liter jug (3, 4).
- Empty the 3-liter jug. Now, you have exactly 4 liters in the 5-liter jug (0, 4).
Reference: The Die Hard example.
Example 2:
Input: x = 2, y = 6, target = 5
Output: false
Example 3:
Input: x = 1, y = 2, target = 3
Output: true
Explanation: Fill both jugs. The total amount of water in both jugs is equal to 3 now.
Constraints:
1 <= x, y, target <= 103
代码结果
运行时间: 34 ms, 内存: 16.0 MB
/*
题目思路:
1. 如果x + y < target,那么无论如何都无法达到目标。
2. 使用数学方法可以确定是否可以通过水壶操作达到目标。具体来说,如果目标体积是x和y的最大公约数的倍数,并且不超过x和y的总容量,那么就可以实现目标。
3. 因此,问题可以归结为计算x和y的最大公约数,然后检查target是否是其倍数。
*/
import java.util.stream.IntStream;
public class WaterJugProblemStream {
public boolean canMeasureWater(int x, int y, int target) {
if (x + y < target) return false;
return target % IntStream.iterate(x, n -> n > 0, n -> y % n).filter(n -> y % n == 0).findFirst().getAsInt() == 0;
}
public static void main(String[] args) {
WaterJugProblemStream wjp = new WaterJugProblemStream();
System.out.println(wjp.canMeasureWater(3, 5, 4)); // true
System.out.println(wjp.canMeasureWater(2, 6, 5)); // false
System.out.println(wjp.canMeasureWater(1, 2, 3)); // true
}
}
解释
方法:
这个题解利用了贝祖定理(Bézout's identity)的性质。贝祖定理指出,对于任意两个整数 x 和 y,存在整数 a 和 b 使得 ax + by = gcd(x, y),其中 gcd(x, y) 表示 x 和 y 的最大公约数。
题解的思路是:
1. 如果目标水量 target 大于两个水壶的容量之和 x+y,那么无论如何都无法达到目标水量,返回 False。
2. 否则,检查目标水量 target 是否可以被 x 和 y 的最大公约数整除。如果可以整除,那么可以通过某种倒水方案达到目标水量;否则无法达到目标水量。
时间复杂度:
O(1)
空间复杂度:
O(1)
代码细节讲解
🦆
在题解中,如何理解贝祖定理在此问题中的应用,特别是其中的ax + by = gcd(x, y)表达式如何与倒水操作关联?
▷🦆
题解提到如果目标水量大于两个水壶的容量之和,结果为False。能否详细解释为什么这种情况下不能达到目标水量?
▷🦆
题解中未详细阐述如何实际操作倒水以达到目标水量。能否提供一个具体的操作步骤例子,说明如何利用两个水壶的最大公约数达到目标水量?
▷