forked from keineahnung2345/leetcode-cpp-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1114. Print in Order.py
160 lines (107 loc) · 5.14 KB
/
1114. Print in Order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//https://leetcode.com/problems/print-in-order/discuss/335939/5-Python-threading-solutions-(Barrier-Lock-Event-Semaphore-Condition)-with-explanation
//Runtime: 28 ms, faster than 96.04% of Python3 online submissions for Print in Order.
//Memory Usage: 13.1 MB, less than 100.00% of Python3 online submissions for Print in Order.
from threading import Barrier
class Foo:
def __init__(self):
self.first_barrier = Barrier(2)
self.second_barrier = Barrier(2)
def first(self, printFirst: 'Callable[[], None]') -> None:
# printFirst() outputs "first". Do not change or remove this line.
printFirst()
self.first_barrier.wait()
def second(self, printSecond: 'Callable[[], None]') -> None:
self.first_barrier.wait()
# printSecond() outputs "second". Do not change or remove this line.
printSecond()
self.second_barrier.wait()
def third(self, printThird: 'Callable[[], None]') -> None:
self.second_barrier.wait()
# printThird() outputs "third". Do not change or remove this line.
printThird()
//Runtime: 40 ms, faster than 59.25% of Python3 online submissions for Print in Order.
//Memory Usage: 13.1 MB, less than 100.00% of Python3 online submissions for Print in Order.
from threading import Lock
class Foo:
def __init__(self):
self.locks = (Lock(), Lock())
self.locks[0].acquire()
self.locks[1].acquire()
def first(self, printFirst: 'Callable[[], None]') -> None:
# printFirst() outputs "first". Do not change or remove this line.
printFirst()
self.locks[0].release()
def second(self, printSecond: 'Callable[[], None]') -> None:
# printSecond() outputs "second". Do not change or remove this line.
with self.locks[0]:
printSecond()
self.locks[1].release()
def third(self, printThird: 'Callable[[], None]') -> None:
# printThird() outputs "third". Do not change or remove this line.
with self.locks[1]:
printThird()
//Runtime: 40 ms, faster than 59.25% of Python3 online submissions for Print in Order.
//Memory Usage: 13.1 MB, less than 100.00% of Python3 online submissions for Print in Order.
from threading import Event
class Foo:
def __init__(self):
self.done = (Event(), Event())
def first(self, printFirst: 'Callable[[], None]') -> None:
# printFirst() outputs "first". Do not change or remove this line.
printFirst()
self.done[0].set()
def second(self, printSecond: 'Callable[[], None]') -> None:
# printSecond() outputs "second". Do not change or remove this line.
self.done[0].wait()
printSecond()
self.done[1].set()
def third(self, printThird: 'Callable[[], None]') -> None:
# printThird() outputs "third". Do not change or remove this line.
self.done[1].wait()
printThird()
/Runtime: 44 ms, faster than 44.53% of Python3 online submissions for Print in Order.
//Memory Usage: 13 MB, less than 100.00% of Python3 online submissions for Print in Order.
from threading import Semaphore
class Foo:
def __init__(self):
self.gates = (Semaphore(0), Semaphore(0))
def first(self, printFirst: 'Callable[[], None]') -> None:
# printFirst() outputs "first". Do not change or remove this line.
printFirst()
self.gates[0].release()
def second(self, printSecond: 'Callable[[], None]') -> None:
# printSecond() outputs "second". Do not change or remove this line.
with self.gates[0]:
printSecond()
self.gates[1].release()
def third(self, printThird: 'Callable[[], None]') -> None:
# printThird() outputs "third". Do not change or remove this line.
with self.gates[1]:
printThird()
//Runtime: 44 ms, faster than 44.53% of Python3 online submissions for Print in Order.
//Memory Usage: 13 MB, less than 100.00% of Python3 online submissions for Print in Order.
from threading import Condition
class Foo:
def __init__(self):
self.exec_condition = Condition()
self.order = 0
self.first_finish = lambda : self.order == 1
self.second_finish = lambda : self.order == 2
def first(self, printFirst: 'Callable[[], None]') -> None:
# printFirst() outputs "first". Do not change or remove this line.
with self.exec_condition:
printFirst()
self.order = 1
self.exec_condition.notify(2)
def second(self, printSecond: 'Callable[[], None]') -> None:
# printSecond() outputs "second". Do not change or remove this line.
with self.exec_condition:
self.exec_condition.wait_for(self.first_finish)
printSecond()
self.order = 2
self.exec_condition.notify()
def third(self, printThird: 'Callable[[], None]') -> None:
# printThird() outputs "third". Do not change or remove this line.
with self.exec_condition:
self.exec_condition.wait_for(self.second_finish)
printThird()