All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0301_0400.s0365_water_and_jug_problem.Solution Maven / Gradle / Ivy

There is a newer version: 1.24
Show newest version
package g0301_0400.s0365_water_and_jug_problem;

// #Medium #Math #Depth_First_Search #Breadth_First_Search

public class Solution {
    private int gcd(int n1, int n2) {
        if (n2 == 0) {
            return n1;
        }
        return gcd(n2, n1 % n2);
    }

    public boolean canMeasureWater(int jug1, int jug2, int target) {
        if (jug1 + jug2 < target) {
            return false;
        }
        int gcd = gcd(jug1, jug2);
        return target % gcd == 0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy