I like to read this solution as a small machine: keep the useful information, throw away the noise. For 1117. Building H2O, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
The notes already sitting in the source point us in the right direction:
- https://leetcode.com/problems/building-h2o/discuss/721793/CORRECT-and-efficient-C%2B%2B-solution-with-mutex-and-condition-variables.-99
- https://github.com/keineahnung2345/cpp-code-snippets/blob/master/thread/conditional_variable_notify_one.cpp
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are reset, hydrogen, oxygen.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//https://leetcode.com/problems/building-h2o/discuss/721793/CORRECT-and-efficient-C%2B%2B-solution-with-mutex-and-condition-variables.-99
02//https://github.com/keineahnung2345/cpp-code-snippets/blob/master/thread/conditional_variable_notify_one.cpp
03//Runtime: 172 ms, faster than 77.24% of C++ online submissions for Building H2O.
04//Memory Usage: 9.2 MB, less than 18.16% of C++ online submissions for Building H2O.
05class H2O {
06private:
07 int waitH, waitO;
08 int needH, needO;
09 condition_variable cvH, cvO;
10
11 mutex mtx;
12 bool building;
13
14 void reset(){
15 needH = 2;
16 needO = 1;
17 building = false;
18 }
19public:
20 H2O() {
21 waitH = waitO = 0;
22 needH = 2;
23 needO = 1;
24 building = false;
25 }
26
27 void hydrogen(function<void()> releaseHydrogen) {
28 unique_lock<mutex> ul(mtx);
29 ++waitH;
30 //condition 1: not building and the needed resource is collected
31 //condition 2: still building and need H
32 cvH.wait(ul, [&](){return
33 (!building && waitH >= 2 && waitO >= 1) || (building && needH > 0);});
34
35 building = true;
36 --waitH; //this H thread stop waiting
37 --needH; //this H thread is used, so need one less H
38
39 //wake up a H thread to continue building
40 if(needH > 0) cvH.notify_one();
41
42 //wake up a O thread to continue building
43 if(needO > 0) cvO.notify_one();
44
45 //complete building
46 if(needH == 0 && needO == 0) reset();
47
48 // releaseHydrogen() outputs "H". Do not change or remove this line.
49 releaseHydrogen();
50 }
51
52 void oxygen(function<void()> releaseOxygen) {
53 unique_lock<mutex> ul(mtx);
54 ++waitO;
55 //condition 2: still building and need O
56 cvO.wait(ul, [&](){return
57 (!building && waitH >= 2 && waitO >= 1) || (building && needO > 0);});
58
59 building = true;
60 --waitO;
61 --needO;
62
63 /*
64 need one or more H, we can notify all because after a thread is waken up,
65 it will decrease needH, and so the condition 2 in cvH will be updated
66 */
67 if(needH > 0) cvH.notify_all();
68
69 if(needH == 0 && needO == 0) reset();
70
71 // releaseOxygen() outputs "O". Do not change or remove this line.
72 releaseOxygen();
73 }
74};
Cost