My accepted Java solution. Math. Modulo theory.


  • 0
    O
    public boolean canMeasureWater(int x, int y, int z) {
    	    if (z == 0) return true;
    	    if (z > x + y) return false;
    	    if (x == 0 && y == 0) return false;
    	    if (z == x || y == z || x + y == z) return true;
    	    int rest = 0;
    	    if (x == 0 || y == 0) {
    	        return x == 0 ? z == y : z == x;
    	    }
    	    int less = x < y ? x : y;
    	    int great = less == x ? y : x;
    	    rest = great%less;
    	    if (rest == 0) {
    	        return false;
    	    }
    	    if (z%rest == 0) {
    	        return true;
    	    }
    	    int lRest = rest;
    	    while(less%lRest != 0) {
    	    	if(z%(less%lRest) == 0) {
    	    		return true;
    	    	}
    	    	lRest = less%lRest;
    	    }
    	    int rRest = rest;
    	    while(great%rRest != 0) {
    	    	if(z%(great%rRest) == 0) {
    	    		return true;
    	    	}
    	    	rRest = less%rRest;
    	    }
    	    return false;
        }
    

  • 1
    G

    Hi @Oleksii-Danylevskyi,
    Little bit of explanation will be really helpful.


Log in to reply
 

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