class Solution:
def canMeasureWater(self, x: int, y: int, z: int) -> bool:
if x + y < z:
return False
queue = [(0, 0)]
seen = set((0, 0))
while(len(queue) > 0):
a, b = queue.pop(0)
if a ==z or b == z or a + b == z:
return True
states = set()
states.add((x, b))
states.add((a, y))
states.add((0, b))
states.add((a, 0))
states.add((min(x, b + a), 0 if b < x - a else b - (x - a)))
states.add((0 if a + b < y else a - (y - b), min(b + a, y)))
for state in states:
if state in seen:
continue;
queue.append(state)
seen.add(state)
return False
class Solution:
def canMeasureWater(self, x: int, y: int, z: int) -> bool:
if x + y < z:
return False
if (z == 0):
return True
if (x == 0):
return y == z
if (y == 0):
return x == z
def GCD(a, b):
smaller = min(a, b)
while smaller:
if a % smaller == 0 and b % smaller == 0:
return smaller
smaller -= 1
return z % GCD(x, y) == 0
/**
* @param {number} x
* @param {number} y
* @param {number} z
* @return {boolean}
*/
var canMeasureWater = function (x, y, z) {
if (x + y < z) return false;
if (z === 0) return true;
if (x === 0) return y === z;
if (y === 0) return x === z;
function GCD(a, b) {
let min = Math.min(a, b);
while (min) {
if (a % min === 0 && b % min === 0) return min;
min--;
}
return 1;
}
return z % GCD(x, y) === 0;
};
def GCD(a, b):
if b == 0: return a
return GCD(b, a % b)