Algorithm


Problem Name: 1117. Building H2O

There are two kinds of threads: oxygen and hydrogen. 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 releaseHydrogen and releaseOxygen methods respectively, which will allow them to pass the barrier. These threads should pass the barrier in groups of three, and they must immediately bond with each other to form a water molecule. You must guarantee that all the threads from one molecule bond before any other threads from the next molecule do.

In other words:

  • If an oxygen thread arrives at the barrier when no hydrogen threads are present, it must wait for two hydrogen threads.
  • If a hydrogen thread arrives at the barrier when no other threads are present, it must wait for an oxygen thread and another hydrogen thread.

We do not have to worry about matching the threads up explicitly; the threads do not necessarily know which other threads they are paired up with. The key is that threads pass the barriers in complete sets; thus, if we examine the sequence of threads that bind and divide them into groups of three, each group should contain one oxygen and two hydrogen threads.

Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.

 

Example 1:

Input: water = "HOH"
Output: "HHO"
Explanation: "HOH" and "OHH" are also valid answers.

Example 2:

Input: water = "OOHHHH"
Output: "HHOHHO"
Explanation: "HOHHHO", "OHHHHO", "HHOHOH", "HOHHOH", "OHHHOH", "HHOOHH", "HOHOHH" and "OHHOHH" are also valid answers.

 

Constraints:

  • 3 * n == water.length
  • 1 <= n <= 20
  • water[i] is either 'H' or 'O'.
  • There will be exactly 2 * n 'H' in water.
  • There will be exactly n 'O' in water.

Code Examples

#1 Code Example with Python Programming

Code - Python Programming


from threading import Lock

class H2O(object):
    def __init__(self):
        self.H = 0
        self.O = 0
        self.mu = Lock()
        pass


    def hydrogen(self, releaseHydrogen):
        self.releaseHydrogen = releaseHydrogen
        with self.mu:
            self.H += 1
            self.ouput()

    def oxygen(self, releaseOxygen):
        self.releaseOxygen = releaseOxygen
        with self.mu:
            self.O += 1
            self.ouput()
        
    def ouput(self):
        while self.ok():
            self.releaseHydrogen()
            self.releaseHydrogen()
            self.releaseOxygen()
            self.H -= 2
            self.O -= 1
    
    def ok(self):
        return self.H >= 2 and self.O >= 1
Copy The Code & Try With Live Editor

Input

x
+
cmd
water = "HOH"

Output

x
+
cmd
"HHO"
Advertisements

Demonstration


Previous
#1116 Leetcode Print Zero Even Odd Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#1122 Leetcode Relative Sort Array Solution in C, C++, Java, JavaScript, Python, C# Leetcode