← Home

1116. Print Zero Even Odd

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
111

The trick here is to name the state correctly, then let the implementation follow. For 1116. Print Zero Even Odd, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. 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/print-zero-even-odd/discuss/358630/C%2B%2B-Solution

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 zero, even, odd.

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:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

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

solution.cpp
01//Runtime: 24 ms, faster than 91.61% of C++ online submissions for Print Zero Even Odd.
02//Memory Usage: 9.2 MB, less than 100.00% of C++ online submissions for Print Zero Even Odd.
03//https://leetcode.com/problems/print-zero-even-odd/discuss/358630/C%2B%2B-Solution
04
05class ZeroEvenOdd {
06private:
07    int n;
08    mutex mtx;
09    condition_variable cv;
10    //used to choose next thread
11    int next; //-1 for zero, 1 for odd, 0 for even
12    //the next integer to print
13    int i;
14public:
15    ZeroEvenOdd(int n) {
16        this->n = n;
17        this->next = -1;
18        this->i = 1;
19    }
20
21    // printNumber(x) outputs "x", where x is an integer.
22    void zero(function<void(int)> printNumber) {
23        //need a while loop
24        while(this->i <= this->n){
25            unique_lock<mutex> lck(mtx);
26            cv.wait(lck, [this](){return this->next == -1;});
27            //need to check whether i is valid before printing
28            if(this-> i <= this->n){
29                printNumber(0);
30            }
31            //this line should be put outside "if" block!
32            //, so other threads can exit
33            this->next = i%2;
34            //need "unlock" here!
35            lck.unlock();
36            cv.notify_all();
37        }
38
39        return;
40    }
41
42    void even(function<void(int)> printNumber) {
43        while(this->i <= this->n){
44            unique_lock<mutex> lck(mtx);
45            cv.wait(lck, [this](){return this->next == 0;});
46            if(this-> i <= this->n){
47                printNumber(i);
48            }
49            this->i += 1;
50            this->next = -1;
51            lck.unlock();
52            cv.notify_all();
53        }
54        return;
55    }
56
57    void odd(function<void(int)> printNumber) {
58        while(this->i <= this->n){
59            unique_lock<mutex> lck(mtx);
60            cv.wait(lck, [this](){return this->next == 1;});
61            if(this-> i <= this->n){
62                printNumber(i);
63            }
64            this->i += 1;
65            this->next = -1;
66            lck.unlock();
67            cv.notify_all();
68        }
69        return;
70    }
71};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.