Class Solution

java.lang.Object
g0301_0400.s0365_water_and_jug_problem.Solution

public class Solution extends Object
365 - Water and Jug Problem.<p>Medium</p> <p>You are given two jugs with capacities <code>jug1Capacity</code> and <code>jug2Capacity</code> liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly <code>targetCapacity</code> liters using these two jugs.</p> <p>If <code>targetCapacity</code> liters of water are measurable, you must have <code>targetCapacity</code> liters of water contained <strong>within one or both buckets</strong> by the end.</p> <p>Operations allowed:</p> <ul> <li>Fill any of the jugs with water.</li> <li>Empty any of the jugs.</li> <li>Pour water from one jug into another till the other jug is completely full, or the first jug itself is empty.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> jug1Capacity = 3, jug2Capacity = 5, targetCapacity = 4</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> The famous <a href="https://www.youtube.com/watch?v=BVtQNK_ZUJg&ab_channel=notnek01" target="_top">Die Hard</a> example</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> jug1Capacity = 2, jug2Capacity = 6, targetCapacity = 5</p> <p><strong>Output:</strong> false</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> jug1Capacity = 1, jug2Capacity = 2, targetCapacity = 3</p> <p><strong>Output:</strong> true</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= jug1Capacity, jug2Capacity, targetCapacity <= 10<sup>6</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • canMeasureWater

      public boolean canMeasureWater(int jug1, int jug2, int target)