mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
42 lines
1.5 KiB
HTML
42 lines
1.5 KiB
HTML
|
<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> </p>
|
||
|
<p><strong>Example 1:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> jug1Capacity = 3, jug2Capacity = 5, targetCapacity = 4
|
||
|
<strong>Output:</strong> true
|
||
|
<strong>Explanation:</strong> The famous <a href="https://www.youtube.com/watch?v=BVtQNK_ZUJg&ab_channel=notnek01" target="_blank">Die Hard</a> example
|
||
|
</pre>
|
||
|
|
||
|
<p><strong>Example 2:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> jug1Capacity = 2, jug2Capacity = 6, targetCapacity = 5
|
||
|
<strong>Output:</strong> false
|
||
|
</pre>
|
||
|
|
||
|
<p><strong>Example 3:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> jug1Capacity = 1, jug2Capacity = 2, targetCapacity = 3
|
||
|
<strong>Output:</strong> true
|
||
|
</pre>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong>Constraints:</strong></p>
|
||
|
|
||
|
<ul>
|
||
|
<li><code>1 <= jug1Capacity, jug2Capacity, targetCapacity <= 10<sup>6</sup></code></li>
|
||
|
</ul>
|