Water and Jug Problem

Problem: Water and Jug Problem

The problem can be easily mapped to a greatest common divisor problem.;

Code in Python:

class Solution(object):
    def canMeasureWater(self, x, y, z):
        dmax = max(x, y)
        dmin = min(x, y)
        if dmin ==0 and dmax ==z or z ==0:
            return True
        elif dmin == 0 and dmax != z or x+y<z:
            return False
        resid = self.gcd(dmax, dmin)
        return True if z % resid == 0 else False

    def gcd(self, x, y):
        return x if y == 0  else self.gcd(y, x%y)

results matching ""

    No results matching ""