zoukankan      html  css  js  c++  java
  • LeetCode 1117. Building H2O

    原题链接在这里:https://leetcode.com/problems/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 be able to 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 has to wait for two hydrogen threads.
    • 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.

    We don’t have to worry about matching the threads up explicitly; that is, the threads do not necessarily know which other threads they are paired up with. The key is just that threads pass the barrier in complete sets; thus, if we examine the sequence of threads that bond 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: "HOH"
    Output: "HHO"
    Explanation: "HOH" and "OHH" are also valid answers.
    

    Example 2:

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

    Constraints:

    • Total length of input string will be 3n, where 1 ≤ n ≤ 20.
    • Total number of H will be 2n in the input string.
    • Total number of O will be n in the input string.

    题解:

    Used synchronized on one lock. When for H runnable count == 2, H needs to wait, lock.wait().

    Otherwise, H run, count++, notifyAll.

    When for O runnable, count != 2, O needs to wait, lock wait().

    Otherwise, O run, count = 0, notifyAll.

    Time Complexity: O(1).

    Space: O(1).

    AC Java:

     1 class H2O {
     2     private Object lock = new Object();
     3     private int count = 0;
     4     
     5     public H2O() {
     6         
     7     }
     8 
     9     public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
    10         synchronized(lock){
    11             while(count == 2){
    12                 lock.wait();
    13             }
    14             
    15             // releaseHydrogen.run() outputs "H". Do not change or remove this line.
    16             releaseHydrogen.run();
    17             count++;
    18             lock.notifyAll();
    19         }
    20     }
    21 
    22     public void oxygen(Runnable releaseOxygen) throws InterruptedException {
    23         synchronized(lock){
    24             while(count != 2){
    25                 lock.wait();
    26             }
    27             
    28             // releaseOxygen.run() outputs "O". Do not change or remove this line.
    29             releaseOxygen.run();
    30             count = 0;
    31             lock.notifyAll();
    32         }
    33     }
    34 }
  • 相关阅读:
    docker 部署springcloud项目【限制容器内存、CPU】
    docker容器中安装vim 、telnet、ifconfig、ping、curl命令
    开源仓库Harbor搭建及配置过程
    动手学深度学习 | PyTorch神经网络基础 | 14
    动手学深度学习 | 实战:Kaggle房价预测+课程竞赛:加州2020年房价预测 | 13
    动手学深度学习 | 数值稳定性+模型初始化和激活函数 | 12
    动手学深度学习 | 丢弃法 | 11
    动手学深度学习 | 模型选择+过拟合和欠拟合 | 09
    动手学深度学习 | 多层感知机+代码实现 | 08
    动手学深度学习 | Softmax回归+损失函数+图片分类数据集 | 07
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12302140.html
Copyright © 2011-2022 走看看