bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1114. Print in Order

1114. Print in Order

Leetcode Solutions

Approach: Synchronization with Locks and Conditions

  1. Initialize a lock and two condition variables, cond1 and cond2.
  2. Initialize two boolean flags, firstFinished and secondFinished, to False.
  3. In the first() method, acquire the lock, execute the print statement, set firstFinished to True, signal cond1, and release the lock.
  4. In the second() method, acquire the lock, while firstFinished is False, wait on cond1, execute the print statement, set secondFinished to True, signal cond2, and release the lock.
  5. In the third() method, acquire the lock, while secondFinished is False, wait on cond2, execute the print statement, and release the lock.
UML Thumbnail

Approach: Synchronization with Semaphores

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...