Skip to content
LC-1114 Easy LeetCode

1114. Print in Order

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 71% Topics: Concurrency
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

import threading


class Foo(object):
    def __init__(self):
        self.__cv = threading.Condition()
        self.__has_first = False
        self.__has_second = False

    def first(self, printFirst):
        """
        :type printFirst: method
        :rtype: void
        """
        with self.__cv:
            # printFirst() outputs "first". Do not change or remove this line.
            printFirst()            
            self.__has_first = True
            self.__cv.notifyAll()

    def second(self, printSecond):
        """
        :type printSecond: method
        :rtype: void
        """
        with self.__cv:
            while not self.__has_first:
                self.__cv.wait()
            # printSecond() outputs "second". Do not change or remove this line.
            printSecond()
            self.__has_second = True
            self.__cv.notifyAll()
              
    def third(self, printThird):
        """
        :type printThird: method
        :rtype: void
        """
        with self.__cv:
            while not self.__has_second:
                self.__cv.wait()
            # printThird() outputs "third". Do not change or remove this line.
            printThird()
            self.__cv.notifyAll()
        

Solution from kamyu104/LeetCode-Solutions · MIT