0ms solution exploiting gcd


  • 0
    I
    public boolean canMeasureWater(int x, int y, int z) {
            if(z > x + y){
                return false;
            }
            if(z == 0) return true;
            if(x == 0 && y == 0 ){
                return false;
            }
            if(x == 0){
                return z % y == 0;
            }
            if(y == 0){
                return z % x == 0;
            }
            if(z % x == 0 || z % y == 0) return true;
            int gcd = gcd(x, y);
            if(z % gcd != 0) return false;
            return true;
        }
        
        public int gcd(int a, int b) {
            if (b==0) return a;
            return gcd(b,a%b);
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.