前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 1117 - Building H2O

LeetCode 1117 - Building H2O

作者头像
Reck Zhang
发布2021-08-11 11:02:28
6140
发布2021-08-11 11:02:28
举报
文章被收录于专栏:Reck Zhang

Building H2O

Desicription

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:

代码语言:javascript
复制
Input: "HOH"
Output: "HHO"
Explanation: "HOH" and "OHH" are also valid answers.

Example 2:

代码语言:javascript
复制
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.

Solution

代码语言:javascript
复制
class H2O {
private:
    std::mutex mutex_{};
    std::condition_variable condition_variable_{};
    int step = 0;

public:
    H2O() {

    }

    void hydrogen(const std::function<void()>& releaseHydrogen) {

        std::unique_lock unique_lock_{mutex_};
        condition_variable_.wait(unique_lock_, [&](){return step <= 1;});
        // releaseHydrogen() outputs "H". Do not change or remove this line.
        releaseHydrogen();
        step += 1;
        condition_variable_.notify_all();
    }

    void oxygen(const std::function<void()>& releaseOxygen) {

        std::unique_lock unique_lock_{mutex_};
        condition_variable_.wait(unique_lock_, [&](){return step == 2;});
        // releaseOxygen() outputs "O". Do not change or remove this line.
        releaseOxygen();
        step = 0;
        condition_variable_.notify_all();
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-12-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Building H2O
    • Desicription
      • Solution
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档