Class Solution
-
- All Implemented Interfaces:
public final class Solution365 - Water and Jug Problem.
Medium
You are given two jugs with capacities
jug1Capacityandjug2Capacityliters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactlytargetCapacityliters using these two jugs.If
targetCapacityliters of water are measurable, you must havetargetCapacityliters of water contained within one or both buckets by the end.Operations allowed:
Fill any of the jugs with water.
Empty any of the jugs.
Pour water from one jug into another till the other jug is completely full, or the first jug itself is empty.
Example 1:
Input: jug1Capacity = 3, jug2Capacity = 5, targetCapacity = 4
Output: true
Explanation: The famous Die Hard example
Example 2:
Input: jug1Capacity = 2, jug2Capacity = 6, targetCapacity = 5
Output: false
Example 3:
Input: jug1Capacity = 1, jug2Capacity = 2, targetCapacity = 3
Output: true
Constraints:
<code>1 <= jug1Capacity, jug2Capacity, targetCapacity <= 10<sup>6</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final BooleancanMeasureWater(Integer jug1Capacity, Integer jug2Capacity, Integer targetCapacity)-
-
Method Detail
-
canMeasureWater
final Boolean canMeasureWater(Integer jug1Capacity, Integer jug2Capacity, Integer targetCapacity)
-
-
-
-