1114. Print in Order LeetCode Solution

In this guide, you will get 1114. Print in Order LeetCode Solution with the best time and space complexity. The solution to Print in Order problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Print in Order solution in C++
  4. Print in Order solution in Java
  5. Print in Order solution in Python
  6. Additional Resources
1114. Print in Order LeetCode Solution image

Problem Statement of Print in Order

Suppose we have a class:

public class Foo {
public void first() { print(“first”); }
public void second() { print(“second”); }
public void third() { print(“third”); }
}

The same instance of Foo will be passed to three different threads. Thread A will call first(), thread B will call second(), and thread C will call third(). Design a mechanism and modify the program to ensure that second() is executed after first(), and third() is executed after second().
Note:
We do not know how the threads will be scheduled in the operating system, even though the numbers in the input seem to imply the ordering. The input format you see is mainly to ensure our tests’ comprehensiveness.

Example 1:

Input: nums = [1,2,3]
Output: “firstsecondthird”
Explanation: There are three threads being fired asynchronously. The input [1,2,3] means thread A calls first(), thread B calls second(), and thread C calls third(). “firstsecondthird” is the correct output.

Example 2:

Input: nums = [1,3,2]
Output: “firstsecondthird”
Explanation: The input [1,3,2] means thread A calls first(), thread B calls third(), and thread C calls second(). “firstsecondthird” is the correct output.

See also  811. Subdomain Visit Count LeetCode Solution

Constraints:

nums is a permutation of [1, 2, 3].

Complexity Analysis

  • Time Complexity: Google AdSense
  • Space Complexity: Google Analytics

1114. Print in Order LeetCode Solution in C++

class Foo {
 public:
  Foo() {
    firstDone.lock();
    secondDone.lock();
  }

  void first(function<void()> printFirst) {
    printFirst();
    firstDone.unlock();
  }

  void second(function<void()> printSecond) {
    firstDone.lock();
    printSecond();
    secondDone.unlock();
  }

  void third(function<void()> printThird) {
    secondDone.lock();
    printThird();
  }

 private:
  mutex firstDone;
  mutex secondDone;
};
/* code provided by PROGIEZ */

1114. Print in Order LeetCode Solution in Java

class Foo {
  public void first(Runnable printFirst) throws InterruptedException {
    printFirst.run();
    firstDone.incrementAndGet();
  }

  public void second(Runnable printSecond) throws InterruptedException {
    while (firstDone.get() != 1)
      ;
    printSecond.run();
    secondDone.incrementAndGet();
  }

  public void third(Runnable printThird) throws InterruptedException {
    while (secondDone.get() != 1)
      ;
    printThird.run();
  }

  private AtomicInteger firstDone = new AtomicInteger();
  private AtomicInteger secondDone = new AtomicInteger();
}
// code provided by PROGIEZ

1114. Print in Order LeetCode Solution in Python

from threading import Lock


class Foo:
  def __init__(self):
    self.firstDone = Lock()
    self.secondDone = Lock()
    self.firstDone.acquire()
    self.secondDone.acquire()

  def first(self, printFirst: 'Callable[[], None]') -> None:
    printFirst()
    self.firstDone.release()

  def second(self, printSecond: 'Callable[[], None]') -> None:
    self.firstDone.acquire()
    printSecond()
    self.secondDone.release()

  def third(self, printThird: 'Callable[[], None]') -> None:
    self.secondDone.acquire()
    printThird()
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.