|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author Openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](https://github.com/openset/leetcode/tree/master/problems/print-zero-even-odd "Print Zero Even Odd") |
| 9 | + |
| 10 | +Next > |
| 11 | + |
| 12 | +## 1117. Building H2O (Hard) |
| 13 | + |
| 14 | +<p>There are two kinds of threads, <code>oxygen</code> and <code>hydrogen</code>. Your goal is to group these threads to form water molecules. There is a barrier where each thread has to wait until a complete molecule can be formed. Hydrogen and oxygen threads will be given a <code>releaseHydrogen</code> and <code>releaseOxygen</code> method respectfully, which will allow them to pass the barrier. These threads should pass the barrier in groups of three, and they must be able to immediately bond with each other to form a water molecule. You must guarantee that all the threads from one molecule bond <em>before</em> any other threads from the next molecule do.</p> |
| 15 | + |
| 16 | +<p>In other words:</p> |
| 17 | + |
| 18 | +<ul> |
| 19 | + <li>If an oxygen thread arrives at the barrier when no hydrogen threads are present, it has to wait for two hydrogen threads.</li> |
| 20 | + <li>If a hydrogen thread arrives at the barrier when no other threads are present, it has to wait for an oxygen thread and another hydrogen thread.</li> |
| 21 | +</ul> |
| 22 | + |
| 23 | +<p>Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.</p> |
| 24 | + |
| 25 | +<div> |
| 26 | +<p> </p> |
| 27 | +</div> |
| 28 | + |
| 29 | +<div> |
| 30 | +<p><strong>Example 1:</strong></p> |
| 31 | + |
| 32 | +<pre> |
| 33 | +<strong>Input: </strong><span id="example-input-1-1">"HOH"</span> |
| 34 | +<strong>Output: </strong><span id="example-output-1">"HHO" |
| 35 | +<strong>Explanation:</strong> "HOH" and "OHH" are also valid answers.</span> |
| 36 | +</pre> |
| 37 | + |
| 38 | +<div> |
| 39 | +<p><strong>Example 2:</strong></p> |
| 40 | + |
| 41 | +<pre> |
| 42 | +<strong>Input: </strong><span id="example-input-1-1">"OOHHHH"</span> |
| 43 | +<strong>Output: </strong><span id="example-output-1">"HHOHHO" |
| 44 | +<strong>Explanation:</strong> "HOHHHO", "OHHHHO", "HHOHOH", "HOHHOH", "OHHHOH", "HHOOHH", "HOHOHH" and "OHHOHH" are also valid answers.</span> |
| 45 | +</pre> |
| 46 | +</div> |
| 47 | +</div> |
| 48 | + |
| 49 | +<p> </p> |
| 50 | +<p><strong>Constraints:</strong></p> |
| 51 | + |
| 52 | +<ul> |
| 53 | + <li>Total length of input string will be 3<em>n</em>, where 1 ≤ <em>n</em> ≤ 50.</li> |
| 54 | + <li>Total number of <code>H</code> will be 2<em>n</em> in the input string.</li> |
| 55 | + <li>Total number of <code>O</code> will be <em>n</em> in the input string.</li> |
| 56 | +</ul> |
0 commit comments