Let's make this one less mysterious. For 1114. Print in Order, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are first, second, third.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- 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//Runtime: 1624 ms, faster than 10.44% of C++ online submissions for Print in Order.
02//Memory Usage: 9.2 MB, less than 100.00% of C++ online submissions for Print in Order.
03
04class Foo {
05public:
06 int count;
07
08 Foo() {
09 count = 0;
10 }
11
12 void first(function<void()> printFirst) {
13
14 // printFirst() outputs "first". Do not change or remove this line.
15 printFirst();
16 count += 1;
17 }
18
19 void second(function<void()> printSecond) {
20 while(count < 1){
21 }
22 // printSecond() outputs "second". Do not change or remove this line.
23 printSecond();
24 count += 1;
25 }
26
27 void third(function<void()> printThird) {
28 while(count < 2){
29
30 }
31 // printThird() outputs "third". Do not change or remove this line.
32 printThird();
33 count += 1;
34 }
35};
36
37//https://leetcode.com/problems/print-in-order/discuss/343384/C%2B%2BWhy-most-of-the-solutions-using-mutex-are-wrong%2Bsolution
38//Runtime: 152 ms, faster than 63.21% of C++ online submissions for Print in Order.
39//Memory Usage: 9.3 MB, less than 100.00% of C++ online submissions for Print in Order.
40class Foo {
41public:
42 int count;
43 mutex mtx;
44 condition_variable cv;
45
46 Foo() {
47 count = 1;
48 }
49
50 void first(function<void()> printFirst) {
51 unique_lock<mutex> lck(mtx);
52 // printFirst() outputs "first". Do not change or remove this line.
53 printFirst();
54 count += 1;
55 cv.notify_all();
56 }
57
58 void second(function<void()> printSecond) {
59 unique_lock<mutex> lck(mtx);
60 // printSecond() outputs "second". Do not change or remove this line.
61 cv.wait(lck, [this](){return count == 2;});
62 printSecond();
63 count += 1;
64 cv.notify_all();
65 }
66
67 void third(function<void()> printThird) {
68 unique_lock<mutex> lck(mtx);
69 // printThird() outputs "third". Do not change or remove this line.
70 cv.wait(lck, [this](){return count == 3;});
71 printThird();
72 count += 1;
73 cv.notify_all();
74 }
75};
Cost