question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
maximum-number-of-tasks-you-can-assign
C++ greedy + binary search
c-greedy-binary-search-by-robbinb1993-xzef
\nclass Solution {\npublic:\n bool pos(const int M, vector<int>& tasks, vector<int>& workers, int pills, int strength) { \n multiset<int> W;\n
robbinb1993
NORMAL
2022-02-20T12:51:15.510738+00:00
2022-02-20T12:51:15.510768+00:00
341
false
```\nclass Solution {\npublic:\n bool pos(const int M, vector<int>& tasks, vector<int>& workers, int pills, int strength) { \n multiset<int> W;\n for (int i = int(workers.size()) - 1; i >= int(workers.size()) - M; i--)\n W.insert(workers[i]);\n \n for (int i = M - 1; i >...
0
0
[]
0
maximum-number-of-tasks-you-can-assign
c++ solution using binary search
c-solution-using-binary-search-by-dilips-nmsb
\nclass Solution {\npublic:\n bool find(vector<int>&t,vector<int>&w,int mid,int pill,int s)\n {\n multiset<int>st(w.begin(),w.end());\n for(
dilipsuthar17
NORMAL
2022-01-28T15:23:00.390105+00:00
2022-01-28T15:23:00.390143+00:00
521
false
```\nclass Solution {\npublic:\n bool find(vector<int>&t,vector<int>&w,int mid,int pill,int s)\n {\n multiset<int>st(w.begin(),w.end());\n for(int i=mid-1;i>=0;i--)\n {\n auto it=st.lower_bound(t[i]);\n if(it==st.end())\n {\n it=st.lower_bound(t...
0
0
['C', 'Binary Tree', 'C++']
0
maximum-number-of-tasks-you-can-assign
C++ | Multiset | Binary Search
c-multiset-binary-search-by-tanishbothra-q279
\nclass Solution {\npublic:\n int maxTaskAssign(vector<int>& tasks, vector<int>& workers, int pills, int strength) {\n \n sort(begin(tasks),end(tas
tanishbothra22
NORMAL
2021-12-27T16:04:42.770372+00:00
2021-12-27T16:07:55.273127+00:00
390
false
```\nclass Solution {\npublic:\n int maxTaskAssign(vector<int>& tasks, vector<int>& workers, int pills, int strength) {\n \n sort(begin(tasks),end(tasks));\n sort(begin(workers),end(workers),greater<int>());\n \n \n int n=tasks.size();\n int m=workers.size();\n \n ...
0
0
['C']
0
maximum-number-of-tasks-you-can-assign
Why does example 3 return '2' instead of '3'
why-does-example-3-return-2-instead-of-3-f4we
For the 3rd example in the problem description, why does the output expect 2 instead of 3? (0-indexed) Worker 1 can be assigned to task 0, worker 2 can be assig
springathing
NORMAL
2021-12-16T05:12:08.659775+00:00
2021-12-16T05:24:36.100191+00:00
102
false
For the 3rd example in the problem description, why does the output expect 2 instead of 3? (0-indexed) Worker 1 can be assigned to task 0, worker 2 can be assigned to task 1 with 1 pill boost, and worker 3 can be assigned to task 2 with 2 pill boosts.\n```\nInput: tasks = [10,15,30], workers = [0,10,10,10,10], pills = ...
0
0
[]
1
maximum-number-of-tasks-you-can-assign
C# Solution
c-solution-by-springathing-o9b0
Binary Search\n\npublic class Solution {\n public int MaxTaskAssign(int[] t, int[] w, int p, int s) {\n Array.Sort(t);\n Array.Sort(w);\n
springathing
NORMAL
2021-12-16T05:08:25.355572+00:00
2021-12-16T05:08:25.355612+00:00
129
false
Binary Search\n```\npublic class Solution {\n public int MaxTaskAssign(int[] t, int[] w, int p, int s) {\n Array.Sort(t);\n Array.Sort(w);\n \n int l = 0, r = Math.Min(t.Length, w.Length);\n while (l + 1 < r) {\n int m = l + (r - l) / 2;\n if (CanAssign(t, w, ...
0
0
[]
0
maximum-number-of-tasks-you-can-assign
Python 3 solution
python-3-solution-by-dhananjay79-6hao
After watching striver\'s live coding, I understood the intution behind solving this problem.\nI am dumb :|\n\nclass Solution:\n def maxTaskAssign(self, T:
dhananjay79
NORMAL
2021-12-09T20:42:15.053039+00:00
2021-12-09T20:42:15.053082+00:00
220
false
After watching striver\'s live coding, I understood the intution behind solving this problem.\nI am dumb :|\n```\nclass Solution:\n def maxTaskAssign(self, T: List[int], W: List[int], pills: int, strength: int) -> int:\n \n \n def isPossible(T,W,pills,strength):\n\n harderTask, stron...
0
0
[]
0
maximum-number-of-tasks-you-can-assign
Same multiset solution but a bit different approach gets WA
same-multiset-solution-but-a-bit-differe-cswf
My solution a bit different but do not know why it doesn\'t work. \n\n1) Start from hardest task. \n2) Find weakest worker that can do the task without pill\n3)
tahlil
NORMAL
2021-11-20T03:42:27.495453+00:00
2021-11-20T03:53:24.448332+00:00
239
false
My solution a bit different but do not know why it doesn\'t work. \n\n1) Start from hardest task. \n2) Find weakest worker that can do the task without pill\n3) If step 2 fails then find the weakest worker with pill can do it.\n4) If succeeds then remove the worker. Otherwise try the next task.\n\n```\nclass Solution {...
0
0
[]
1
maximum-number-of-tasks-you-can-assign
[Golang] Can somebody help me to point out why my solution fails at test cases 33?
golang-can-somebody-help-me-to-point-out-yebz
My logic: Do binary search to choose the potential answer k. \n\n1) choose the smallest k tasks and strongest k workers. try to assign it without pill first (fi
shentianjie466357304
NORMAL
2021-11-20T03:42:00.055901+00:00
2021-11-20T03:45:35.829179+00:00
103
false
My logic: Do binary search to choose the potential answer k. \n\n1) choose the smallest k tasks and strongest k workers. try to assign it without pill first (find the weakest worker possible greedy strategy)\n2) If not try to find the weakest worker with a pill \n3) Otherwise it is impossible to assign k tasks, return ...
0
0
[]
0
maximum-number-of-tasks-you-can-assign
Scala, use a TreeMap for greedy algorithm when validating on the binary search
scala-use-a-treemap-for-greedy-algorithm-1pdv
scala\n\n def maxTaskAssign(tasks: Array[Int], workers: Array[Int], pills: Int, strength: Int): Int = {\n type int = Int\n var res = 0\n tasks.sortInP
qiuqiushasha
NORMAL
2021-11-18T19:10:35.769002+00:00
2021-11-18T19:10:35.769029+00:00
65
false
```scala\n\n def maxTaskAssign(tasks: Array[Int], workers: Array[Int], pills: Int, strength: Int): Int = {\n type int = Int\n var res = 0\n tasks.sortInPlaceWith((a, b) => a <= b)\n workers.sortInPlaceWith((a, b) => a <= b)\n\n import scala.collection.mutable.TreeMap\n\n val tl = tasks.length\n va...
0
0
[]
0
maximum-number-of-tasks-you-can-assign
Rust binary search + BTreeMap
rust-binary-search-btreemap-by-robetking-6cnd
Would be nice to have a multiset in rust but can use BTreeMap with counts.\n\nIdea to check if it\'s possible in a binary search iteration: take the K smallest
robetkingnz
NORMAL
2021-11-14T16:36:39.463116+00:00
2021-11-14T16:39:13.856870+00:00
315
false
Would be nice to have a multiset in rust but can use BTreeMap with counts.\n\nIdea to check if it\'s possible in a binary search iteration: take the K smallest tasks and the K strongest workers. Go through the workers left to right assigning the weakest worker to the easiest task. If the worker can\'t do the easiest ta...
0
0
[]
0
maximum-number-of-tasks-you-can-assign
Easy C++ Solution| Binary Search | MultiSet
easy-c-solution-binary-search-multiset-b-uldz
{Based on solution by @astroash}\nWe first do binary search on the solution space, i.e, 0 to minimum of tasks.size or workers.size().\n\nThe condition we check
harit_1
NORMAL
2021-11-14T08:18:19.162272+00:00
2021-11-14T08:19:48.434542+00:00
217
false
*{Based on solution by @astroash}*\nWe first do binary search on the solution space, i.e, 0 to minimum of tasks.size or workers.size().\n\nThe condition we check in iteration is it possible to assign the first k smallest tasks to the workers?\n\nFor each task, there can be three cases:\n- A worker without the pill can ...
0
0
['Binary Search', 'C']
0
maximum-number-of-tasks-you-can-assign
c++ nlg^2n
c-nlg2n-by-colinyoyo26-39cs
\nclass Solution {\npublic:\n int maxTaskAssign(vector<int>& tasks, vector<int>& ws, int ps, int st) {\n sort(tasks.begin(), tasks.end());\n so
colinyoyo26
NORMAL
2021-11-14T01:10:48.240610+00:00
2021-11-14T01:10:48.240659+00:00
157
false
```\nclass Solution {\npublic:\n int maxTaskAssign(vector<int>& tasks, vector<int>& ws, int ps, int st) {\n sort(tasks.begin(), tasks.end());\n sort(ws.begin(), ws.end(), greater<int>());\n int m = tasks.size(), n = ws.size();\n auto isvalid = [&] (int k) {\n if (k > min(m, n))...
0
1
[]
0
fizz-buzz-multithreaded
Java, using synchronized with short explanation.
java-using-synchronized-with-short-expla-dyp1
[Code is at the end]\n\nSo I just keep a shared currentNumber showing where we\'re up to. Each thread has to then check whether or not it should take its turn.
hai_dee
NORMAL
2019-09-19T04:08:01.108833+00:00
2019-09-19T04:08:01.108871+00:00
19,022
false
[Code is at the end]\n\nSo I just keep a shared ```currentNumber``` showing where we\'re up to. Each thread has to then check whether or not it should take its turn. If it\'s not the current thread\'s turn, it calls ```wait()```, thus suspending and giving up the monitor. After each increment of ```currentNumber```, we...
126
2
[]
12
fizz-buzz-multithreaded
python, >99.28%, a standard Lock() based solution, with detailed explanation
python-9928-a-standard-lock-based-soluti-0ph0
\nimport threading\n\nclass FizzBuzz:\n def __init__(self, n: int): \n self.n = n\n self.f = threading.Lock()\n self.b = threading.
rmoskalenko
NORMAL
2020-03-18T01:27:11.901818+00:00
2020-08-28T03:40:10.206829+00:00
8,136
false
```\nimport threading\n\nclass FizzBuzz:\n def __init__(self, n: int): \n self.n = n\n self.f = threading.Lock()\n self.b = threading.Lock()\n self.fb = threading.Lock()\n self.f.acquire()\n self.b.acquire()\n self.fb.acquire()\n self.main = threading.Lock...
70
1
[]
10
fizz-buzz-multithreaded
Java implementation using Semaphore as token 4ms
java-implementation-using-semaphore-as-t-t6xs
\n\nclass FizzBuzz {\n private int n;\n\tprivate Semaphore fizz = new Semaphore(0);\n\tprivate Semaphore buzz = new Semaphore(0);\n\tprivate Semaphore fizzbu
franklee9527
NORMAL
2019-10-08T06:45:44.867795+00:00
2019-10-08T06:45:44.867831+00:00
6,127
false
\n```\nclass FizzBuzz {\n private int n;\n\tprivate Semaphore fizz = new Semaphore(0);\n\tprivate Semaphore buzz = new Semaphore(0);\n\tprivate Semaphore fizzbuzz = new Semaphore(0);\n\tprivate Semaphore num = new Semaphore(1);\n\n\tpublic FizzBuzz(int n) {\n this.n = n;\n }\n\n\t// printFizz.run() outputs...
69
0
[]
10
fizz-buzz-multithreaded
C++ mutex / condition_variable, please suggest improvements
c-mutex-condition_variable-please-sugges-w5p0
```\nprivate:\n int n;\n int count;\n mutex m;\n condition_variable cv;\n\npublic:\n FizzBuzz(int n) {\n this->n = n;\n this->count
grokus
NORMAL
2019-09-22T16:10:36.308025+00:00
2019-09-22T16:10:36.308063+00:00
8,977
false
```\nprivate:\n int n;\n int count;\n mutex m;\n condition_variable cv;\n\npublic:\n FizzBuzz(int n) {\n this->n = n;\n this->count = 1;\n }\n\n void fizz(function<void()> printFizz) {\n while (true) {\n unique_lock<mutex> lock(m);\n while (count <= n && (...
48
1
[]
15
fizz-buzz-multithreaded
Python 3 non-cheesy solution with semaphores
python-3-non-cheesy-solution-with-semaph-p138
My solution is similar to other Semaphore solutions, but it doesn\'t need math formulas to calculate how many times we should print fizz or buzz.\nI feel that i
ilgor
NORMAL
2020-01-08T02:32:03.195872+00:00
2020-01-08T02:32:03.195921+00:00
6,126
false
My solution is similar to other Semaphore solutions, but it doesn\'t need math formulas to calculate how many times we should print fizz or buzz.\nI feel that it\'s a code smell and not true to the spirit of multi-threading.\nInstead I used **done** flag which is set by controlling thread and allows slave threads to pe...
37
0
['Python', 'Python3']
7
fizz-buzz-multithreaded
C++ condition_variable waiting on predicate
c-condition_variable-waiting-on-predicat-sm1t
\nclass\xA0FizzBuzz\xA0{\n\xA0\xA0\xA0\xA0private:\n\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0int\xA0n;\n\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0atomic<int>\xA0current;\n\xA0\xA0
igorb
NORMAL
2020-01-20T00:09:24.602439+00:00
2020-01-20T00:09:24.602494+00:00
3,139
false
```\nclass\xA0FizzBuzz\xA0{\n\xA0\xA0\xA0\xA0private:\n\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0int\xA0n;\n\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0atomic<int>\xA0current;\n\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0mutex\xA0mx;\n\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0condition_variable\xA0cv;\n\xA0\xA0\xA0\xA0public:\n\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0Fiz...
19
0
['C']
4
fizz-buzz-multithreaded
Java Solutions with Explanation (Approach 1 - object lock, Approach 2- Semaphore)
java-solutions-with-explanation-approach-c9sk
Intuition\nFour separate threads is supposed to operate on thie FizzBuzz class. We need to guard the block of code that has the logic to decide if the number is
mitesh_pv
NORMAL
2023-01-22T04:15:06.173971+00:00
2023-01-22T11:47:44.685466+00:00
3,933
false
# Intuition\nFour separate threads is supposed to operate on thie FizzBuzz class. We need to guard the block of code that has the logic to decide if the number is multiple of 3&5, 3|5 or none.\n\n# Approach 1 - Using Object Lock\nUsing two instance variables, `n` which is the the last number, `i` which is the currrent ...
18
0
['Concurrency', 'Java']
6
fizz-buzz-multithreaded
Java non-blocking solution using AtomicInteger
java-non-blocking-solution-using-atomici-cent
java\nimport java.util.ConcurrentModificationException;\nimport java.util.concurrent.atomic.AtomicInteger;\nimport java.util.function.IntConsumer;\n\nclass Fizz
dimkagorhover
NORMAL
2019-09-19T19:16:35.155375+00:00
2019-09-19T19:16:35.155423+00:00
4,929
false
```java\nimport java.util.ConcurrentModificationException;\nimport java.util.concurrent.atomic.AtomicInteger;\nimport java.util.function.IntConsumer;\n\nclass FizzBuzz {\n\n private final AtomicInteger counter;\n\n private final int n;\n\n public FizzBuzz(int n) {\n this.n = n;\n counter = new At...
17
1
['Java']
8
fizz-buzz-multithreaded
Java: Fun and simple CyclicBarrier solution
java-fun-and-simple-cyclicbarrier-soluti-5oan
In this solution we use a CyclicBarrier to hold each iteration of the while loop until each thread has an opportunity to execute. Once all four threads have exe
Reves
NORMAL
2020-03-15T16:06:36.052020+00:00
2020-03-15T16:11:48.963624+00:00
943
false
In this solution we use a CyclicBarrier to hold each iteration of the while loop until each thread has an opportunity to execute. Once all four threads have executed, the Barrier is tripped, the counter increased, and all threads advance to the next iteration.\n\n\n```\nclass FizzBuzz {\n private int n;\n private...
14
0
[]
4
fizz-buzz-multithreaded
Java Using Lock (Mutex Simulation)
java-using-lock-mutex-simulation-by-mjay-1nxy
Logic is pretty simple, just lock the mutex before starting any operation, check the condition, if true do the desired task, if not unlock the mutex so it can b
mjay4
NORMAL
2020-08-04T05:54:30.699444+00:00
2020-08-04T05:56:20.116390+00:00
2,656
false
Logic is pretty simple, just lock the mutex before starting any operation, check the condition, if true do the desired task, if not unlock the mutex so it can be used by some other thread. For every mutex repeat the same operation.\nOne thing to note, if our count is more than n we need to unlock the mutex before it r...
13
1
['Java']
1
fizz-buzz-multithreaded
Python 3 | 5 Approaches (Lock/Semaphore/Event/Condition) | Explanation
python-3-5-approaches-locksemaphoreevent-leqp
Explanation\n- Semaphore(1) can be used to replace Lock(), therefore 5 approaches\n- Very similar to 1116. Print Zero Even Odd\n- Make sure we have control over
idontknoooo
NORMAL
2022-12-27T06:55:37.025076+00:00
2022-12-27T07:17:23.560418+00:00
1,876
false
## Explanation\n- `Semaphore(1)` can be used to replace `Lock()`, therefore 5 approaches\n- Very similar to [1116. Print Zero Even Odd](https://leetcode.com/problems/print-zero-even-odd/description/)\n- Make sure we have control over the executing order by using `Lock/Semaphore/Condition/Lock`.\n- The `Barrier` solutio...
11
0
['Concurrency', 'Python3']
2
fizz-buzz-multithreaded
Python beats 100% using Semaphore
python-beats-100-using-semaphore-by-utad-npcm
When we know N, we know how many times 3\'s multiple appears, which is N/3. We also know N/5 is the number of 5\'s multiple, N/15 is the number of 15\'s multipl
utada
NORMAL
2019-09-19T03:30:21.238094+00:00
2019-09-19T03:52:13.910123+00:00
2,891
false
When we know N, we know how many times 3\'s multiple appears, which is N/3. We also know N/5 is the number of 5\'s multiple, N/15 is the number of 15\'s multiple. \nTotally, we print fuzzbuzz N/15 times, print fuzz N/3-N-15 times, and print buzz N/5-N/15 times.\nWe firstly execute thread function number(),and according...
11
3
['Python']
2
fizz-buzz-multithreaded
Multi-threaded FizzBuzz using Mutex and Condition Variable [O(n)]
multi-threaded-fizzbuzz-using-mutex-and-kfphb
Intuition\n Describe your first thoughts on how to solve this problem. \nThe code uses a mutex and condition variable to synchronize the output of the different
freddyt18
NORMAL
2023-04-29T14:04:49.810372+00:00
2023-04-29T14:04:49.810416+00:00
1,965
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe code uses a mutex and condition variable to synchronize the output of the different methods (fizz, buzz, fizzbuzz, and number) and ensure that they are executed in the correct order. \n\nThe wait conditions for each method are defined...
9
0
['C++']
1
fizz-buzz-multithreaded
Python, Condition, wait_for()
python-condition-wait_for-by-marboni-qy7j
\nimport threading\nfrom functools import partial\n\nclass FizzBuzz:\n def __init__(self, n: int):\n self.n = n\n self.cur = 1\n self.co
marboni
NORMAL
2020-12-20T20:13:22.718978+00:00
2020-12-20T20:13:22.719011+00:00
903
false
```\nimport threading\nfrom functools import partial\n\nclass FizzBuzz:\n def __init__(self, n: int):\n self.n = n\n self.cur = 1\n self.condition = threading.Condition()\n\n # printFizz() outputs "fizz"\n def fizz(self, printFizz: \'Callable[[], None]\') -> None:\n \tself._execute(lamb...
9
1
[]
0
fizz-buzz-multithreaded
C++ Very Simple Solution Using 3 Semaphores
c-very-simple-solution-using-3-semaphore-ci6d
\n#include <semaphore.h>\nclass FizzBuzz {\nprivate:\n int n;\n sem_t fizz_sem;\n sem_t buzz_sem;\n sem_t fizzbuzz_sem;\n\npublic:\n FizzBuzz(int
yehudisk
NORMAL
2020-11-19T18:29:25.800975+00:00
2020-11-19T18:29:25.801015+00:00
1,235
false
```\n#include <semaphore.h>\nclass FizzBuzz {\nprivate:\n int n;\n sem_t fizz_sem;\n sem_t buzz_sem;\n sem_t fizzbuzz_sem;\n\npublic:\n FizzBuzz(int n) {\n this->n = n;\n sem_init(&fizz_sem, 0, 0);\n sem_init(&buzz_sem, 0, 0);\n sem_init(&fizzbuzz_sem, 0, 0);\n }\n \n ...
9
6
['C']
4
fizz-buzz-multithreaded
JAVA using Synchronized
java-using-synchronized-by-shivbaba-5w89
\nTaking a shared variable - count. Protecting it using synchronized. If condition is not correct, wait(). Else print, increase the count and notify other waiti
shivbaba
NORMAL
2021-07-18T17:37:21.296062+00:00
2021-07-18T17:37:43.041001+00:00
1,592
false
\nTaking a shared variable - count. Protecting it using synchronized. If condition is not correct, wait(). Else print, increase the count and notify other waiting threads so that they can resume running. \n\nclass FizzBuzz {\n private int n;\n private int count;\n\n public FizzBuzz(int n) {\n this.n = n...
8
0
['Java']
2
fizz-buzz-multithreaded
Easy Mutex Solution || Love Babbar Approach || Best Way 🔥🔥
easy-mutex-solution-love-babbar-approach-q2k6
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
abhi_pandit_18
NORMAL
2023-07-13T06:09:11.805113+00:00
2023-07-13T06:09:11.805158+00:00
1,339
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
7
0
['C++']
3
fizz-buzz-multithreaded
Java wait/notifyAll
java-waitnotifyall-by-softarium-lsuu
\nclass FizzBuzz {\n private final int max;\n private int counter = 1;\n public FizzBuzz(int n) { this.max = n; }\n\n private synchronized void sync
softarium
NORMAL
2021-12-03T21:02:49.845025+00:00
2021-12-03T21:02:49.845051+00:00
578
false
```\nclass FizzBuzz {\n private final int max;\n private int counter = 1;\n public FizzBuzz(int n) { this.max = n; }\n\n private synchronized void synchronizedPrint(final Runnable print, final IntPredicate predicate) throws InterruptedException {\n while (counter <= max) {\n if (predicate....
7
0
[]
2
fizz-buzz-multithreaded
Java - 8 ms - Use a semaphore with one permit
java-8-ms-use-a-semaphore-with-one-permi-grjy
\nclass FizzBuzz {\n private static int NUMBER_OF_PRINT_LICENSES = 1;\n \n private int n;\n private int printIndex;\n private Semaphore printLice
ed-karabinus
NORMAL
2021-04-18T07:06:43.201147+00:00
2021-04-18T07:06:43.201190+00:00
928
false
```\nclass FizzBuzz {\n private static int NUMBER_OF_PRINT_LICENSES = 1;\n \n private int n;\n private int printIndex;\n private Semaphore printLicense;\n\n public FizzBuzz(int n) {\n this.n = n;\n this.printIndex = 1;\n this.printLicense = new Semaphore(NUMBER_OF_PRINT_LICENSES);...
7
0
['Java']
4
fizz-buzz-multithreaded
Beats 100% Other Solutions
beats-100-other-solutions-by-day_tripper-8b69
\nimport threading\n\nclass FizzBuzz:\n def __init__(self, n: int): \n self.n = n\n self.f = threading.Lock()\n self.b = threading.
Day_Tripper
NORMAL
2022-07-21T03:41:39.874544+00:00
2022-07-21T03:41:39.874588+00:00
1,314
false
```\nimport threading\n\nclass FizzBuzz:\n def __init__(self, n: int): \n self.n = n\n self.f = threading.Lock()\n self.b = threading.Lock()\n self.fb = threading.Lock()\n self.f.acquire()\n self.b.acquire()\n self.fb.acquire()\n self.main = threading.Lock...
6
0
[]
0
fizz-buzz-multithreaded
C++, single mutex, but not spinlock or roundrobin
c-single-mutex-but-not-spinlock-or-round-x8vz
Each thread checks for its unique condition, so it\'s safe but slower\n\nclass FizzBuzz {\nprivate:\n int n;\n int current_number;\n mutex number_mutex
nalivai
NORMAL
2020-09-09T20:18:25.881308+00:00
2020-09-09T20:21:39.877324+00:00
1,328
false
Each thread checks for its unique condition, so it\'s safe but slower\n```\nclass FizzBuzz {\nprivate:\n int n;\n int current_number;\n mutex number_mutex;\n const int number_of_threads = 4;\n bool done = true;\n \npublic:\n FizzBuzz(int n) {\n this->n = n;\n if (n > 0){\n ...
6
0
['C', 'C++']
2
fizz-buzz-multithreaded
C# SemaphoreSlim Array
c-semaphoreslim-array-by-olegbelousov77-ci49
csharp\nusing System.Threading;\n\npublic class FizzBuzz {\n \n private enum FizzBuzzEnum {\n Fizz = 0,\n Buzz = 1,\n FizzBuzz = 2,\n
olegbelousov77
NORMAL
2021-11-21T21:13:27.148870+00:00
2021-11-21T21:13:27.148922+00:00
658
false
```csharp\nusing System.Threading;\n\npublic class FizzBuzz {\n \n private enum FizzBuzzEnum {\n Fizz = 0,\n Buzz = 1,\n FizzBuzz = 2,\n Number = 3\n }\n \n private int _n;\n private int _k;\n private SemaphoreSlim[] _semaphores;\n private CancellationTokenSource _cts...
5
0
['C#']
1
fizz-buzz-multithreaded
Python3 four locks
python3-four-locks-by-mxmb-vp6p
python\nimport threading as t\nclass FizzBuzz:\n def __init__(self, n: int):\n self.n = n\n self.count = 1\n self.num_lock, self.fizz_lo
mxmb
NORMAL
2021-08-21T18:57:49.406449+00:00
2021-08-21T19:01:07.556241+00:00
937
false
```python\nimport threading as t\nclass FizzBuzz:\n def __init__(self, n: int):\n self.n = n\n self.count = 1\n self.num_lock, self.fizz_lock, self.buzz_lock, self.fizzbuzz_lock = t.Lock(), t.Lock(), t.Lock(), t.Lock()\n self.fizz_lock.acquire()\n self.buzz_lock.acquire()\n ...
5
0
['Python', 'Python3']
1
fizz-buzz-multithreaded
Java Solution with Locks
java-solution-with-locks-by-anjana9-o0it
\nclass FizzBuzz {\n private int n;\n private int count;\n private Object lock;\n\n public FizzBuzz(int n) {\n this.n = n;\n this.coun
anjana9
NORMAL
2020-10-12T05:49:06.750774+00:00
2020-10-12T05:56:42.269903+00:00
1,289
false
```\nclass FizzBuzz {\n private int n;\n private int count;\n private Object lock;\n\n public FizzBuzz(int n) {\n this.n = n;\n this.count = 1;\n this.lock = new Object();\n }\n\n // printFizz.run() outputs "fizz".\n public void fizz(Runnable printFizz) throws InterruptedExcept...
5
1
['Java']
6
fizz-buzz-multithreaded
Easy C !! One mutex and One Condition variable.
easy-c-one-mutex-and-one-condition-varia-5gzi
Use the mutex for locking the current number.\n- Fizz() thread sleeps on the condition variable if either the current number is divisible by 5 or not divisible
csd2592
NORMAL
2019-10-13T00:50:11.498111+00:00
2019-10-13T00:50:11.498147+00:00
1,048
false
Use the mutex for locking the current number.\n- Fizz() thread sleeps on the condition variable if either the current number is divisible by 5 or not divisible by 3.\n- Buzz() thread sleeps on the condition variable if either the current number is divisible by 3 or not divisible by 5.\n- FizzBuzz() thread sleeps on the...
5
0
[]
0
fizz-buzz-multithreaded
Python using just 1 Semaphore
python-using-just-1-semaphore-by-lamnguy-38i4
Unlike other solutions which use 4 semaphores, mine uses just one. This sole semaphore acts as a lock that prevent concurrent access to self.cur. Whenever one o
lamnguyen2187
NORMAL
2019-09-20T00:23:41.493327+00:00
2019-09-20T00:23:41.493374+00:00
1,296
false
Unlike other solutions which use 4 semaphores, mine uses just one. This sole semaphore acts as a lock that prevent concurrent access to `self.cur`. Whenever one of `fizz`, `buzz`, `fizzbuzz`, and `number` manages to acquire the lock, it checks if if its turn and act accordingly it if is. Before releasing the lock, it i...
5
3
[]
1
fizz-buzz-multithreaded
Python clean symmetric solution using Condition
python-clean-symmetric-solution-using-co-z2lf
It may not be as fast as the most voted python solution but I think it\'s more concise in a sense that all 4 methods do the same thing but just the condition to
redtree1112
NORMAL
2022-02-26T07:30:23.774228+00:00
2022-02-26T07:30:23.774260+00:00
563
false
It may not be as fast as the most voted python solution but I think it\'s more concise in a sense that all 4 methods do the same thing but just the condition to wait is different.\n\n```\nfrom threading import *\n\nclass FizzBuzz:\n def __init__(self, n: int):\n self.n = n\n self.cur = 1\n self....
4
0
[]
1
fizz-buzz-multithreaded
C++ | Condition Variable
c-condition-variable-by-vineet0692-5lw8
We can take advantage of condition variables predicate feature to our advantage so that only one thread is waken up to do its job.\n\n\nclass FizzBuzz {\nprivat
vineet0692
NORMAL
2021-07-03T16:40:41.273492+00:00
2021-07-03T16:40:41.273532+00:00
459
false
We can take advantage of condition variables predicate feature to our advantage so that only one thread is waken up to do its job.\n\n```\nclass FizzBuzz {\nprivate:\n int n;\n int i;\n mutex mtx;\n condition_variable cv;\n\npublic:\n FizzBuzz(int n) {\n i = 1;\n this->n = n;\n }\n\n ...
4
0
[]
0
fizz-buzz-multithreaded
C++ condition variable based sln, clean code
c-condition-variable-based-sln-clean-cod-y30g
```\nclass FizzBuzz {\nprivate:\n int n;\n\npublic:\n FizzBuzz(int n) {\n this->n = n;\n }\n\n void do_staff(function check, function print)
dmitrii_bokovikov
NORMAL
2021-03-24T17:44:10.391240+00:00
2021-03-24T17:44:10.391281+00:00
573
false
```\nclass FizzBuzz {\nprivate:\n int n;\n\npublic:\n FizzBuzz(int n) {\n this->n = n;\n }\n\n void do_staff(function<bool()> check, function<void()> print) {\n while (true) {\n unique_lock lck(mt);\n cv.wait(lck, [&check, this]() { return std::invoke(check) || current > ...
4
0
[]
1
fizz-buzz-multithreaded
Python3 solution using barrier
python3-solution-using-barrier-by-johnny-gwrs
python\nfrom threading import Barrier\n\n\nclass FizzBuzz:\n def __init__(self, n: int):\n self.n = n\n self.barrier = Barrier(4)\n\n # prin
johnny5
NORMAL
2021-01-01T13:10:13.977714+00:00
2021-01-01T13:10:13.977743+00:00
453
false
```python\nfrom threading import Barrier\n\n\nclass FizzBuzz:\n def __init__(self, n: int):\n self.n = n\n self.barrier = Barrier(4)\n\n # printFizz() outputs "fizz"\n def fizz(self, printFizz: \'Callable[[], None]\') -> None:\n for number in range(1, self.n+1):\n if number % 3 ...
4
0
[]
3
fizz-buzz-multithreaded
C# basic lock approach
c-basic-lock-approach-by-superfooman-oq2a
\npublic class FizzBuzz {\n private int n;\n private static int count = 1;\n private Object baton = new Object();\n\t\n\tpublic FizzBuzz(int n) {\n
superfooman
NORMAL
2020-12-09T04:05:53.134172+00:00
2020-12-09T04:05:53.134222+00:00
306
false
```\npublic class FizzBuzz {\n private int n;\n private static int count = 1;\n private Object baton = new Object();\n\t\n\tpublic FizzBuzz(int n) {\n this.n = n;\n }\n\t\n private void syncRun(Func<int, bool> condition, Action<int> action){\n\t\twhile(true){ \n lock(baton){\n ...
4
0
[]
0
fizz-buzz-multithreaded
C# using Barrier
c-using-barrier-by-polybios-f0k0
\nusing System.Threading;\npublic class FizzBuzz {\n \n private int n;\n private Barrier b = new Barrier(4);\n\n public FizzBuzz(int n)\n {\n
polybios
NORMAL
2020-06-06T17:17:40.531487+00:00
2020-06-06T17:19:24.107535+00:00
562
false
```\nusing System.Threading;\npublic class FizzBuzz {\n \n private int n;\n private Barrier b = new Barrier(4);\n\n public FizzBuzz(int n)\n {\n this.n = n;\n }\n\n // printFizz() outputs "fizz".\n public void Fizz(Action printFizz)\n {\n for (int i = 1; i <= n; ++i)\n {\...
4
0
[]
5
fizz-buzz-multithreaded
Python using Condition and Lock
python-using-condition-and-lock-by-lacg-3cbs
The other solutions I saw used Semaphores, so I am just sharing mine that is using Lock and Condition.\nI hope it can help someone.\nThe idea behind the lock is
lacg
NORMAL
2019-10-14T15:53:20.947549+00:00
2019-10-14T17:02:01.845875+00:00
1,236
false
The other solutions I saw used Semaphores, so I am just sharing mine that is using Lock and Condition.\nI hope it can help someone.\nThe idea behind the lock is to avoid the other threads look at the counter while one of the process updates its value.\nAlso, if one thread have the right condition, all the other threads...
4
0
['Python', 'Python3']
2
fizz-buzz-multithreaded
Python 100% Semaphore
python-100-semaphore-by-awice-y8i1
\nfrom threading import Semaphore\n\nclass FizzBuzz(object):\n def __init__(self, n):\n self.n = n\n self.sem0 = Semaphore(1)\n self.sem
awice
NORMAL
2019-09-21T23:51:47.160892+00:00
2019-09-21T23:51:47.160946+00:00
1,185
false
```\nfrom threading import Semaphore\n\nclass FizzBuzz(object):\n def __init__(self, n):\n self.n = n\n self.sem0 = Semaphore(1)\n self.sem3 = Semaphore(0)\n self.sem5 = Semaphore(0)\n self.sem15 = Semaphore(0)\n\n def fizz(self, printFizz):\n for i in xrange(3, self.n + ...
4
0
[]
0
fizz-buzz-multithreaded
very simple and easy to understand solution
very-simple-and-easy-to-understand-solut-xa3o
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Kishore_raok
NORMAL
2024-04-25T06:08:56.256227+00:00
2024-04-25T06:08:56.256255+00:00
570
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
0
['Java']
2
fizz-buzz-multithreaded
Optimized Lock-Free C++ Solution with atomics
optimized-lock-free-c-solution-with-atom-9gkz
Approach\n- Using cache-line-sized alignment for atomics\n- Using weak memory ordering where possible\n- Using yield() instead of waiting\n\n# Code\n\nclass Fiz
nikmy
NORMAL
2023-02-21T12:25:33.066340+00:00
2023-02-21T12:27:00.066462+00:00
747
false
# Approach\n- Using cache-line-sized alignment for atomics\n- Using weak memory ordering where possible\n- Using `yield()` instead of waiting\n\n# Code\n```\nclass FizzBuzz {\nprivate:\n int n;\n atomic_int i{1};\n\n alignas(64) atomic_bool n_turn {true} ;\n alignas(64) atomic_bool f_turn {false};\n al...
3
0
['Concurrency', 'C++']
3
fizz-buzz-multithreaded
C++ using semaphores
c-using-semaphores-by-anshu2-jh4v
\n# Code\n\n#include <semaphore.h>\n\nclass FizzBuzz {\nprivate:\n int n;\n sem_t sem_f, sem_b, sem_fb, sem_i;\n int f_cnt = 0, b_cnt = 0, fb_cnt = 0,
anshu2
NORMAL
2022-11-17T02:58:01.135086+00:00
2022-11-17T02:58:01.135130+00:00
256
false
\n# Code\n```\n#include <semaphore.h>\n\nclass FizzBuzz {\nprivate:\n int n;\n sem_t sem_f, sem_b, sem_fb, sem_i;\n int f_cnt = 0, b_cnt = 0, fb_cnt = 0, i_cnt = 0;\n int i = 1;\n\npublic:\n FizzBuzz(int n) {\n this->n = n;\n\n sem_init(&sem_f, 0, 0);\n sem_init(&sem_b, 0, 0);\n ...
3
0
['C++']
0
fizz-buzz-multithreaded
_Fizz Buzz Multithreaded Java - no using any classes java.util.concurrent.*
fizz-buzz-multithreaded-java-no-using-an-sqx9
I don\'t get what knowledge "1195. Fizz Buzz Multithreaded problem tries to check.\nHow don\'t write multithreaded or concurrent programs on Java?\n\nRuntime: 8
zazzazzza
NORMAL
2022-08-01T11:46:56.178295+00:00
2022-08-01T11:46:56.178337+00:00
734
false
I don\'t get what knowledge "[1195. Fizz Buzz Multithreaded](https://leetcode.com/problems/fizz-buzz-multithreaded) problem tries to check.\nHow don\'t write multithreaded or concurrent programs on Java?\n\nRuntime: 8 ms, faster than 81.09% of Java online submissions for Fizz Buzz Multithreaded.\nMemory Usage: 41.5 MB,...
3
0
['Java']
3
fizz-buzz-multithreaded
Python3 | Very Simple solution using Barriers (only) | Beats 72%
python3-very-simple-solution-using-barri-8opp
\nfrom threading import Barrier\nclass FizzBuzz:\n def __init__(self, n: int):\n self.n = n\n self.bar = Barrier(4)\n\n # printFizz() output
thunderVan
NORMAL
2022-07-23T22:43:56.260928+00:00
2022-07-23T22:44:47.062075+00:00
1,014
false
```\nfrom threading import Barrier\nclass FizzBuzz:\n def __init__(self, n: int):\n self.n = n\n self.bar = Barrier(4)\n\n # printFizz() outputs "fizz"\n def fizz(self, printFizz: \'Callable[[], None]\') -> None:\n \t\n for i in range(1,self.n+1):\n if i%5!=0 and i%3==0:\n ...
3
0
['Python']
1
fizz-buzz-multithreaded
Java 5ms Simple Semaphores Solution
java-5ms-simple-semaphores-solution-by-a-cr8y
Few pointers:\n1. Semphore(0) makes some threads locked initially\n2. After every fizz, buzz or fizzBuzz we would always print a number.\n\n\n\nclass FizzBuzz {
amma
NORMAL
2022-07-07T00:58:48.586659+00:00
2022-07-07T00:58:48.586718+00:00
388
false
Few pointers:\n1. Semphore(0) makes some threads locked initially\n2. After every fizz, buzz or fizzBuzz we would always print a number.\n\n\n```\nclass FizzBuzz {\n private int n;\n\n private Semaphore number, fizz , buzz, fizzBuzz;\n \n public FizzBuzz(int n) {\n this.n = n;\n\t\tnumber = new Semap...
3
0
['Java']
0
fizz-buzz-multithreaded
Java solution, 99% faster using Lock, includes test code
java-solution-99-faster-using-lock-inclu-6uqi
Java solution, faster than 99% Java submisions, also includes the code that I used for tests below.\n\n\nclass FizzBuzz {\n private final int n;\n private
stefancomanita
NORMAL
2022-03-10T21:45:35.222366+00:00
2022-03-10T21:45:35.222396+00:00
1,514
false
Java solution, faster than 99% Java submisions, also includes the code that I used for tests below.\n\n```\nclass FizzBuzz {\n private final int n;\n private int currentValue = 1;\n private final java.util.concurrent.locks.Lock lock = new java.util.concurrent.locks.ReentrantLock();\n\n public FizzBuzz(int n...
3
0
['Java']
2
fizz-buzz-multithreaded
Java using ReentrantLock and Condition
java-using-reentrantlock-and-condition-b-2qje
This is the essentially the same idea taken from Java, using synchronized with short explanation.. Thanks @Hai_dee for the great explanation there.\n\nJava\ncla
cielong
NORMAL
2022-01-17T03:02:27.252012+00:00
2022-01-17T03:02:27.252055+00:00
613
false
This is the essentially the same idea taken from [Java, using synchronized with short explanation.](https://leetcode.com/problems/fizz-buzz-multithreaded/discuss/385395/Java-using-synchronized-with-short-explanation.). Thanks [@Hai_dee](https://leetcode.com/Hai_dee/) for the great explanation there.\n\n```Java\nclass F...
3
0
['Java']
1
fizz-buzz-multithreaded
Java 's semaphore solution's explaination for most multithread problem
java-s-semaphore-solutions-explaination-m0dpd
Overall, a semaphore is a kind of token counter controlled by us (developer). \n\nEach time, when a thread runs acquire() method, it acuqires , and then, immed
laodasb
NORMAL
2020-12-01T10:59:29.060922+00:00
2020-12-22T23:31:59.252272+00:00
349
false
Overall, a semaphore is a kind of token counter controlled by us (developer). \n\nEach time, when a thread runs acquire() method, it acuqires , and then, **immediately consumes** that permit token, which allows the thread to run but also decrease its permit token by one.\n\nBy contrast, if a thread calls its release()...
3
0
[]
0
fizz-buzz-multithreaded
Python Solution with Condition Variable
python-solution-with-condition-variable-ap2g1
The idea is that each of 4 threads tries to print numbers that it\'s responsible for. fizz prints numbers divisible only by 3, buzz prints numbers divisible onl
saulpayne
NORMAL
2020-05-19T20:57:54.005684+00:00
2020-05-19T20:57:54.005736+00:00
422
false
The idea is that each of 4 threads tries to print numbers that it\'s responsible for. ```fizz``` prints numbers divisible only by 3, ```buzz``` prints numbers divisible only by 5, ```fizzbuzz``` prints numbers divisible by 15, and ```number``` prints remaining numbers. But we have to handle order of prints.\n\n```\nfro...
3
0
[]
1
fizz-buzz-multithreaded
c# faster than 94% (44ms) using AutoResetEvent
c-faster-than-94-44ms-using-autoreseteve-9c5t
csharp\npublic class FizzBuzz\n\t{\n private readonly AutoResetEvent fizz = new AutoResetEvent(false);\n\t\tprivate readonly AutoResetEvent buzz = new Au
giometrix
NORMAL
2020-04-24T17:07:13.092460+00:00
2020-04-24T17:07:13.092513+00:00
404
false
```csharp\npublic class FizzBuzz\n\t{\n private readonly AutoResetEvent fizz = new AutoResetEvent(false);\n\t\tprivate readonly AutoResetEvent buzz = new AutoResetEvent(false);\n\t\tprivate readonly AutoResetEvent buzzComplete = new AutoResetEvent(false);\n\t\tprivate readonly AutoResetEvent fizzbuzz = new AutoR...
3
0
[]
0
fizz-buzz-multithreaded
Python3 using Lock, 96%
python3-using-lock-96-by-nightybear-l8u0
Attached is the solution using Lock, welcome to discuss.\n\npython\n\nfrom threading import Lock\n\nclass FizzBuzz:\n def __init__(self, n: int):\n se
nightybear
NORMAL
2019-11-13T01:31:04.023876+00:00
2019-11-13T01:31:04.023954+00:00
922
false
Attached is the solution using Lock, welcome to discuss.\n\n```python\n\nfrom threading import Lock\n\nclass FizzBuzz:\n def __init__(self, n: int):\n self.n = n\n self.l1 = [Lock() for _ in range(3)]\n self.l2 = [Lock() for _ in range(3)]\n \n # lock all the locks\n for i i...
3
1
['Python3']
0
fizz-buzz-multithreaded
unique_lock and condition variable C++
unique_lock-and-condition-variable-c-by-dikad
class FizzBuzz {\nprivate:\n int n;\n mutex mtx;\n condition_variable cv;\n int tmp; bool a;\n \npublic:\n FizzBuzz(int n) {\n this->n
_CodeSlayer_
NORMAL
2019-10-28T05:03:18.992992+00:00
2019-10-28T05:03:18.993038+00:00
594
false
class FizzBuzz {\nprivate:\n int n;\n mutex mtx;\n condition_variable cv;\n int tmp; bool a;\n \npublic:\n FizzBuzz(int n) {\n this->n = n; this->tmp=1;//this->tmp2=0;\n }\n\n // printFizz() outputs "fizz".\n void fizz(function<void()> printFizz) {\n while(tmp<=n) {\n {...
3
0
['C']
1
fizz-buzz-multithreaded
Intuitive Java Solution With Explanation
intuitive-java-solution-with-explanation-u1uw
Idea\nUse Guarded Blocks and the logic is similar to producer-consumer problem from\nhttps://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.htm
naveen_kothamasu
NORMAL
2019-10-06T01:11:48.496803+00:00
2019-10-06T01:11:48.496855+00:00
481
false
**Idea**\nUse `Guarded Blocks` and the logic is similar to producer-consumer problem from\nhttps://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html\n\n```\nclass FizzBuzz {\n private int n;\n private int counter = 1;\n public FizzBuzz(int n) {\n this.n = n;\n }\n\n // printFizz...
3
0
[]
1
fizz-buzz-multithreaded
C# 52 ms, barrier
c-52-ms-barrier-by-csuporj-fwbn
\tpublic class FizzBuzz\n {\n private int n;\n private int i;\n private System.Threading.Barrier barrier;\n\n public FizzBuzz(int
csuporj
NORMAL
2019-09-22T00:11:50.675205+00:00
2019-09-22T00:16:10.833396+00:00
473
false
\tpublic class FizzBuzz\n {\n private int n;\n private int i;\n private System.Threading.Barrier barrier;\n\n public FizzBuzz(int n)\n {\n this.n = n;\n barrier = new System.Threading.Barrier(4);\n }\n\n // printFizz() outputs "fizz".\n pu...
3
0
[]
2
fizz-buzz-multithreaded
Simple std::barrier solution
simple-stdbarrier-solution-by-user4153oo-t7gv
\n# Code\ncpp []\n#include <barrier>\n\nclass FizzBuzz {\nprivate:\n int n;\n std::barrier<> sync_point{ 4 };\npublic:\n FizzBuzz(int n) {\n thi
user4153oO
NORMAL
2024-10-15T19:12:28.058985+00:00
2024-10-15T19:12:28.059009+00:00
225
false
\n# Code\n```cpp []\n#include <barrier>\n\nclass FizzBuzz {\nprivate:\n int n;\n std::barrier<> sync_point{ 4 };\npublic:\n FizzBuzz(int n) {\n this->n = n;\n }\n\n // printFizz() outputs "fizz".\n void fizz(function<void()> printFizz) {\n for (unsigned i = 1; i <= n; ++i)\n {\n ...
2
0
['C++']
1
fizz-buzz-multithreaded
Java - Sync-step with Phaser
java-sync-step-with-phaser-by-give_me_th-o6mv
Intuition\nThe result for a given number n is deterministic, and only 1 of the operations will produce output. The shared resource is essentially the iteration
give_me_the_formuoli
NORMAL
2024-05-02T17:00:45.195422+00:00
2024-05-02T17:00:45.195444+00:00
396
false
# Intuition\nThe result for a given number `n` is deterministic, and only 1 of the operations will produce output. The shared resource is essentially the iteration order, so we produce synchronization around that. Additionally, there is only a single result for each value `n`, so each call/thread can independently dete...
2
0
['Java']
0
fizz-buzz-multithreaded
Best Java Solution || Beats 100%
best-java-solution-beats-100-by-ravikuma-5idz
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
ravikumar50
NORMAL
2023-09-28T09:30:24.215074+00:00
2023-09-28T09:30:24.215095+00:00
1,308
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
0
['Java']
1
fizz-buzz-multithreaded
C++ EASY SOLUTION
c-easy-solution-by-arpiii_7474-gatw
Code\n\nclass FizzBuzz {\nprivate:\n int n;\n mutex m;\n condition_variable cv;\n int i;\npublic:\n FizzBuzz(int n) {\n this->n = n;\n
arpiii_7474
NORMAL
2023-09-26T05:21:46.909746+00:00
2023-09-26T05:21:46.909770+00:00
677
false
# Code\n```\nclass FizzBuzz {\nprivate:\n int n;\n mutex m;\n condition_variable cv;\n int i;\npublic:\n FizzBuzz(int n) {\n this->n = n;\n this->i=1;\n }\n\n // printFizz() outputs "fizz".\n void fizz(function<void()> printFizz) {\n while(i<=n){\n unique_lock<mut...
2
0
['C++']
0
fizz-buzz-multithreaded
Trivial C++ beats 100%
trivial-c-beats-100-by-shoumikhin-khfl
\n\nclass FizzBuzz {\nprivate:\n int n;\n atomic_int i{1};\n\npublic:\n FizzBuzz(int n) {\n this->n = n;\n }\n\n // printFizz() outputs "f
shoumikhin
NORMAL
2023-02-01T03:50:27.350798+00:00
2023-02-01T03:50:27.350845+00:00
301
false
\n```\nclass FizzBuzz {\nprivate:\n int n;\n atomic_int i{1};\n\npublic:\n FizzBuzz(int n) {\n this->n = n;\n }\n\n // printFizz() outputs "fizz".\n void fizz(function<void()> printFizz) {\n for (auto ii = i.load(); ii <= n; ii = i.load()) {\n if (ii % 3 == 0 && ii % 5 != 0) {...
2
0
['C++']
1
fizz-buzz-multithreaded
Python Barrier Solution
python-barrier-solution-by-christopher71-x80v
Straight forward simple solution.\n\nAll 4 threads work on 1 iteration the loop together concurrently, then step together after all have reached.\n\n```\nfrom t
christopher71
NORMAL
2022-12-05T10:13:37.382238+00:00
2022-12-05T10:13:37.382276+00:00
355
false
Straight forward simple solution.\n\nAll 4 threads work on 1 iteration the loop together concurrently, then step together after all have reached.\n\n```\nfrom threading import Barrier\n\nbarrier = Barrier(4)\n\nclass FizzBuzz:\n def __init__(self, n: int):\n self.n = n + 1\n\n def fizz(self, printFizz: \'C...
2
0
[]
1
fizz-buzz-multithreaded
C++ using mutex, condition_variable, and unique_lock with Core Guideline annotations
c-using-mutex-condition_variable-and-uni-12ka
I added notes and links to the excellent C++ Core Guidelines section on Concurrency.\n\n\nclass FizzBuzz {\npublic:\n explicit FizzBuzz(int n)\n : n(n
user6040iN
NORMAL
2022-09-08T17:31:19.513874+00:00
2022-09-08T17:31:19.513922+00:00
584
false
I added notes and links to the excellent [C++ Core Guidelines section on Concurrency](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#SScp-con).\n\n```\nclass FizzBuzz {\npublic:\n explicit FizzBuzz(int n)\n : n(n), i(1), mutex(), condition()\n {\n }\n\n // printFizz() ou...
2
0
['C']
0
fizz-buzz-multithreaded
Efficient Java Solution using Semaphore
efficient-java-solution-using-semaphore-98azi
\nclass FizzBuzz {\n private int n;\n private int current;\n private int fizzBuzzTotal;\n private int fizzTotal;\n private int buzzTotal;\n pr
HYRFAL
NORMAL
2022-05-07T16:41:14.780270+00:00
2022-05-07T16:41:14.780314+00:00
374
false
```\nclass FizzBuzz {\n private int n;\n private int current;\n private int fizzBuzzTotal;\n private int fizzTotal;\n private int buzzTotal;\n private int numTotal;\n private Semaphore fizzBuzzSem = new Semaphore(0);\n private Semaphore fizzSem = new Semaphore(0);\n private Semaphore buzzSem ...
2
0
[]
1
fizz-buzz-multithreaded
Java 3 solutions
java-3-solutions-by-mypanacea-3u5x
Semaphors\n\n\npublic class Solution1195 {\n\n\tprivate int n;\n\n\tSemaphore fizz = new Semaphore(0);\n\n\tSemaphore buzz = new Semaphore(0);\n\n\tSemaphore fi
mypanacea
NORMAL
2022-02-15T21:26:04.423763+00:00
2022-02-15T21:27:11.120418+00:00
326
false
Semaphors\n\n```\npublic class Solution1195 {\n\n\tprivate int n;\n\n\tSemaphore fizz = new Semaphore(0);\n\n\tSemaphore buzz = new Semaphore(0);\n\n\tSemaphore fizzbuzz = new Semaphore(0);\n\n\tSemaphore number = new Semaphore(1);\n\n\tprivate int count;\n\n\tpublic Solution1195(int n) {\n\t\tthis.n = n;\n\t\tthis.cou...
2
0
[]
0
fizz-buzz-multithreaded
[Java] Poor Coder's Concurrency - no lock, mutex, semaphore, or other nonsense
java-poor-coders-concurrency-no-lock-mut-4h12
```\n// Is this a solution that respects the electricty costs of slamming every thread every millisecond? No.\n// This bad boy is, however, faster than 98.88% o
mikedupuis
NORMAL
2021-12-16T06:56:25.391461+00:00
2021-12-16T07:00:51.278442+00:00
374
false
```\n// Is this a solution that respects the electricty costs of slamming every thread every millisecond? No.\n// This bad boy is, however, faster than 98.88% of other Java submissions as of 12/16/2021.\n// This is poor coder\'s concurrency, where we need not worry about fancy semaphores, mutexes, locks, etc.\n// Fair ...
2
0
['Java']
0
fizz-buzz-multithreaded
Java Synchronized, very easy to understand and implement, faster than 99%
java-synchronized-very-easy-to-understan-zzou
\nimport java.util.function.Function;\nimport java.util.function.IntConsumer;\n\nclass FizzBuzz {\n private int n;\n private int i;\n\n public FizzBuzz
quandang2002
NORMAL
2021-12-05T06:06:17.172692+00:00
2021-12-05T06:15:50.570194+00:00
260
false
```\nimport java.util.function.Function;\nimport java.util.function.IntConsumer;\n\nclass FizzBuzz {\n private int n;\n private int i;\n\n public FizzBuzz(int n) {\n this.n = n;\n this.i = 1;\n }\n\n private synchronized boolean testAndIncrease(Function<Integer, Boolean> fn) {\n if (...
2
0
[]
0
fizz-buzz-multithreaded
JAVA EASY SOLUTION
java-easy-solution-by-aniket7419-r40n
\nclass FizzBuzz {\n private int i;\n private int n=1;\n public FizzBuzz(int n) {\n i = n;\n }\n\n // printFizz.run() outputs "fizz".\n
aniket7419
NORMAL
2021-11-29T13:38:50.290912+00:00
2021-11-29T13:38:50.290950+00:00
251
false
```\nclass FizzBuzz {\n private int i;\n private int n=1;\n public FizzBuzz(int n) {\n i = n;\n }\n\n // printFizz.run() outputs "fizz".\n public void fizz(Runnable printFizz) throws InterruptedException {\n while(n<=i)\n {\n synchronized(FizzBuzz.class){\n if(n...
2
0
[]
0
fizz-buzz-multithreaded
Python3 solution using Barrier
python3-solution-using-barrier-by-barema-5ee7
Most of the solutions on here are more complicated than they need to be. If you use Barrier to make sure that each thread is looping\nthrough n elements in a sy
baremaximum
NORMAL
2021-11-06T07:39:23.814157+00:00
2021-11-06T07:39:23.814198+00:00
149
false
Most of the solutions on here are more complicated than they need to be. If you use Barrier to make sure that each thread is looping\nthrough `n` elements in a synchronized way, the solution isn\'t much different than regular fizzbuzz.\n```\nfrom threading import Barrier\n\nclass FizzBuzz:\n def __init__(self, n: in...
2
0
[]
0
fizz-buzz-multithreaded
Very Simple Java ReentrantLock solution
very-simple-java-reentrantlock-solution-nqiaf
All the methods do the same thing, this can be abstracted out. Acquire the lock, do your business, unlock for others and continue. \n\n\n\nclass FizzBuzz {\n
hughesj919
NORMAL
2021-09-08T03:17:41.550351+00:00
2021-09-08T03:17:41.550391+00:00
291
false
All the methods do the same thing, this can be abstracted out. Acquire the lock, do your business, unlock for others and continue. \n\n```\n\nclass FizzBuzz {\n private int n;\n private int i;\n private ReentrantLock lock;\n\n public FizzBuzz(int n) {\n this.n = n;\n this.i = 1;\n this....
2
0
[]
0
fizz-buzz-multithreaded
Java | Clean code with synchronized helper function
java-clean-code-with-synchronized-helper-ssu8
\nclass FizzBuzz {\n private final int n;\n private int i = 1;\n\n public FizzBuzz(int n) {\n this.n = n;\n }\n \n private synchronized
arruw
NORMAL
2021-07-12T14:50:45.250978+00:00
2021-07-12T14:50:45.251025+00:00
231
false
```\nclass FizzBuzz {\n private final int n;\n private int i = 1;\n\n public FizzBuzz(int n) {\n this.n = n;\n }\n \n private synchronized void helper(Runnable printer, boolean divBy3, boolean divBy5) throws InterruptedException {\n while (i <= n) {\n if ((i % 3 == 0) == divBy...
2
0
[]
0
fizz-buzz-multithreaded
C++ Semaphores / Efficient, Clean, and Easy to Understand
c-semaphores-efficient-clean-and-easy-to-oon4
\n#include <semaphore.h>\n\nclass FizzBuzz {\nprivate:\n int n;\n \n int count = 1;\n \n sem_t sem_fizz;\n sem_t sem_buzz;\n sem_t sem_fizz
unpoopenzoid
NORMAL
2021-07-07T22:31:23.132909+00:00
2021-07-07T22:33:43.671968+00:00
223
false
```\n#include <semaphore.h>\n\nclass FizzBuzz {\nprivate:\n int n;\n \n int count = 1;\n \n sem_t sem_fizz;\n sem_t sem_buzz;\n sem_t sem_fizzbuzz;\n sem_t sem_number;\n \n void notify_next() {\n count++;\n \n if (count > n) {\n sem_post(&sem_fizz);\n ...
2
0
[]
0
fizz-buzz-multithreaded
Java, no locks and non-blocking solution
java-no-locks-and-non-blocking-solution-ce6wl
Optimistic locking/ Conditional writes based solution. \n\n\nclass FizzBuzz {\n private int n;\n private int max;\n\n public FizzBuzz(int n) {\n
azhartas
NORMAL
2021-05-25T10:36:33.485788+00:00
2021-05-25T10:42:45.864862+00:00
471
false
Optimistic locking/ Conditional writes based solution. \n\n```\nclass FizzBuzz {\n private int n;\n private int max;\n\n public FizzBuzz(int n) {\n this.max = n;\n this.n = 1;\n }\n\n // printFizz.run() outputs "fizz".\n public void fizz(Runnable printFizz) throws InterruptedException {\...
2
1
['Java']
1
fizz-buzz-multithreaded
[Python3] using Lock()
python3-using-lock-by-ye15-bdq8
\n\nfrom threading import Lock\n\nclass FizzBuzz:\n def __init__(self, n: int):\n self.n = n\n self.fizzLock = Lock(); self.fizzLock.acquire()\
ye15
NORMAL
2021-05-14T20:19:43.021181+00:00
2021-05-14T20:19:43.021208+00:00
688
false
\n```\nfrom threading import Lock\n\nclass FizzBuzz:\n def __init__(self, n: int):\n self.n = n\n self.fizzLock = Lock(); self.fizzLock.acquire()\n self.buzzLock = Lock(); self.buzzLock.acquire()\n self.fzbzLock = Lock(); self.fzbzLock.acquire()\n self.numberLock = Lock()\n\n # ...
2
0
['Python3']
0
fizz-buzz-multithreaded
C++ MUTEX AND CONDITIONAL VARIABLE 40MS
c-mutex-and-conditional-variable-40ms-by-jcr9
\nclass FizzBuzz {\nprivate:\n int n;\n mutex m;\n condition_variable fizzCond;\n condition_variable buzzCond;\n condition_variable fizzbuzzCond;
abhi_tom
NORMAL
2021-04-11T05:04:27.927081+00:00
2021-04-11T05:04:27.927110+00:00
288
false
```\nclass FizzBuzz {\nprivate:\n int n;\n mutex m;\n condition_variable fizzCond;\n condition_variable buzzCond;\n condition_variable fizzbuzzCond;\n condition_variable numberCond;\n int cnt;\n \n bool onlyDivByThree(int n){\n if(n%3==0 and n%5!=0)\n return true;\n r...
2
0
[]
0
fizz-buzz-multithreaded
[Java] 7ms | 99% using semaphores for each type of 'token writer'
java-7ms-99-using-semaphores-for-each-ty-a6qq
\nclass FizzBuzz {\n private final int n;\n private final Semaphore numberSemaphore = new Semaphore(1);\n private final Semaphore fizzSemaphore = new S
TheRuminator
NORMAL
2021-03-10T14:31:26.933628+00:00
2021-03-10T14:55:09.051900+00:00
480
false
```\nclass FizzBuzz {\n private final int n;\n private final Semaphore numberSemaphore = new Semaphore(1);\n private final Semaphore fizzSemaphore = new Semaphore(0);\n private final Semaphore buzzSemaphore = new Semaphore(0);\n private final Semaphore fizzbuzzSemaphore = new Semaphore(0);\n\n public ...
2
0
[]
1
fizz-buzz-multithreaded
[Java] Just volatile
java-just-volatile-by-jokersun-7r3j
\nclass FizzBuzz {\n\n private volatile int i;\n\n private final int n;\n\n public FizzBuzz(int n) {\n this.i = 1;\n this.n = n;\n }\n
jokersun
NORMAL
2021-02-15T12:49:51.560026+00:00
2021-03-01T02:13:35.698360+00:00
407
false
```\nclass FizzBuzz {\n\n private volatile int i;\n\n private final int n;\n\n public FizzBuzz(int n) {\n this.i = 1;\n this.n = n;\n }\n\n public void fizz(Runnable printFizz) throws InterruptedException {\n for (int j = i; j <= n; j = i) {\n if (j % 3 == 0 && j % 5 != 0)...
2
0
[]
1
fizz-buzz-multithreaded
[Python] Barrier solution with explanation
python-barrier-solution-with-explanation-499p
There are four threads, and they need to interate through 1 to n synchronously, meaning that no thread can advance to 2 until all threads have "processed" 1, an
goldie213
NORMAL
2021-02-11T02:48:29.881314+00:00
2021-02-11T02:48:29.881459+00:00
368
false
There are four threads, and they need to interate through 1 to n synchronously, meaning that no thread can advance to 2 until all threads have "processed" 1, and so on. Therefore we can use Barrier from python, a convenient helper to make every thread wait until all of them reach some condition.\n\n\n```\nimport thread...
2
0
[]
0
fizz-buzz-multithreaded
C Solution: mutex and cond wait and broadcast
c-solution-mutex-and-cond-wait-and-broad-swdr
``\n#include <pthread.h>\ntypedef struct {\n int nsize;\n int cur;\n pthread_mutex_t lock;\n pthread_cond_t cond;\n} FizzBuzz;\n\nFizzBuzz* fizzBuzz
leet_jx
NORMAL
2020-12-31T17:44:21.527989+00:00
2020-12-31T17:44:21.528037+00:00
321
false
```\n#include <pthread.h>\ntypedef struct {\n int nsize;\n int cur;\n pthread_mutex_t lock;\n pthread_cond_t cond;\n} FizzBuzz;\n\nFizzBuzz* fizzBuzzCreate(int n) {\n FizzBuzz* obj=malloc(sizeof(FizzBuzz));\n obj->nsize=n;\n obj->cur=1;\n pthread_mutex_init(&obj->lock, NULL);\n pthread_cond_i...
2
0
[]
0
fizz-buzz-multithreaded
Java - Beats 100% simple solution
java-beats-100-simple-solution-by-maorbr-gwb1
java\n\nclass FizzBuzz {\n\tprivate int n;\n\n\tprivate AtomicInteger counter;\n\n\tpublic FizzBuzz(int n) {\n\t\tthis.n = n;\n\t\tthis.counter = new AtomicInte
maorbril
NORMAL
2020-09-17T23:35:05.844707+00:00
2020-09-17T23:35:05.844746+00:00
378
false
```java\n\nclass FizzBuzz {\n\tprivate int n;\n\n\tprivate AtomicInteger counter;\n\n\tpublic FizzBuzz(int n) {\n\t\tthis.n = n;\n\t\tthis.counter = new AtomicInteger(1);\n\t}\n\n\n // printFizz.run() outputs "fizz".\n\tpublic void fizz(Runnable printFizz) throws InterruptedException {\n\t\twhile (counter.get() <= n...
2
1
[]
2
fizz-buzz-multithreaded
[C++] Mutex-based Solution Explained, ~70% Time, ~65% Space
c-mutex-based-solution-explained-70-time-wryi
So, we have 4 class variables: n to store our the input passed in the constructor, i to keep track of our currently printed number (initially set to 1), a mutex
ajna
NORMAL
2020-08-26T20:20:12.936887+00:00
2020-08-26T20:20:12.936947+00:00
856
false
So, we have 4 class variables: `n` to store our the input passed in the constructor, `i` to keep track of our currently printed number (initially set to `1`), a [mutex](https://en.cppreference.com/w/cpp/thread/mutex) `m` and a [`condition _variable`](https://en.cppreference.com/w/cpp/thread/condition_variable) `c`.\n\...
2
0
['C', 'C++']
1
fizz-buzz-multithreaded
c# semaphore
c-semaphore-by-code-world-7lcb
Key points of problem:\n\n1. Four threads has same object but calls different method.\n2. This require four threads to signal each other for execution. Think it
code-world
NORMAL
2020-08-10T06:11:27.911329+00:00
2020-08-10T06:11:27.911379+00:00
662
false
Key points of problem:\n\n1. Four threads has same object but calls different method.\n2. This require four threads to signal each other for execution. Think it as a traffic signal, traffic from four side has to wait for there signal to go. This makes Semaphore as my pick for this problem.\n\n\n```\nusing System.Thread...
2
0
[]
2
fizz-buzz-multithreaded
Need to notify others when we exit thread task- C++
need-to-notify-others-when-we-exit-threa-1vnd
In many of the C++ posts, I see that we are missing notifying other threads when count > n. Since the order of the execution is never gaurenteed, I belive we ne
swapnai
NORMAL
2020-06-23T01:38:00.992052+00:00
2020-06-23T01:56:00.755429+00:00
175
false
In many of the C++ posts, I see that we are missing notifying other threads when count > n. Since the order of the execution is never gaurenteed, I belive we need to notify others before exiting a thread. \n\n```\nclass FizzBuzz {\nprivate:\n int n;\n int count;\n condition_variable cv;\n mutex mux;\n\npublic:\n ...
2
0
[]
1
fizz-buzz-multithreaded
Java simple semaphore
java-simple-semaphore-by-cuny-66brother-6mwl
\nclass FizzBuzz {\n private int n;\n Semaphore f=new Semaphore(1);\n Semaphore b=new Semaphore(1);\n Semaphore fb=new Semaphore(1);\n Semaphore
CUNY-66brother
NORMAL
2020-05-21T12:15:38.208883+00:00
2020-05-21T12:15:38.208960+00:00
286
false
```\nclass FizzBuzz {\n private int n;\n Semaphore f=new Semaphore(1);\n Semaphore b=new Semaphore(1);\n Semaphore fb=new Semaphore(1);\n Semaphore num=new Semaphore(1);\n public FizzBuzz(int n) {\n this.n = n;\n try{\n f.acquire();\n b.acquire();\n fb.ac...
2
0
[]
0
fizz-buzz-multithreaded
C : simply sema
c-simply-sema-by-universalcoder12-578l
\ntypedef struct {\n sem_t sn, s3, s5, s35; // Individual semaphores for threads\n int i, n;\n} FizzBuzz;\n\nFizzBuzz* fizzBuzzCreate(int n) {\n FizzBu
universalcoder12
NORMAL
2020-03-25T06:55:43.209468+00:00
2020-03-25T07:01:42.304025+00:00
631
false
```\ntypedef struct {\n sem_t sn, s3, s5, s35; // Individual semaphores for threads\n int i, n;\n} FizzBuzz;\n\nFizzBuzz* fizzBuzzCreate(int n) {\n FizzBuzz* obj = malloc(sizeof (FizzBuzz));\n sem_init(&obj->sn, 0, 0); // All three will wait for number-thread to wake them up\n sem_init(&obj->s3, 0, 0);\...
2
0
['C']
0
fizz-buzz-multithreaded
c++ pre-wait solution with mutex and condition_variable
c-pre-wait-solution-with-mutex-and-condi-slgg
with this solution, each thread will lock the mutex and wait only when it is going to print.\n\nclass FizzBuzz {\nprivate:\n int n;\n int cnt;\n mutex
jay6987
NORMAL
2020-03-25T01:21:53.239649+00:00
2020-03-25T01:46:08.862884+00:00
203
false
with this solution, each thread will lock the mutex and wait only when it is going to print.\n```\nclass FizzBuzz {\nprivate:\n int n;\n int cnt;\n mutex mtx;\n condition_variable cv;\n\npublic:\n FizzBuzz(int n) {\n this->n = n;\n this->cnt = 1;\n }\n\n // printFizz() outputs "fizz"....
2
0
[]
0
fizz-buzz-multithreaded
Java use wait and notifyAll()
java-use-wait-and-notifyall-by-hobiter-6z88
\nclass FizzBuzz {\n private int n;\n int curr;\n\n public FizzBuzz(int n) {\n this.n = n;\n curr = 1;\n }\n\n // printFizz.run() o
hobiter
NORMAL
2020-03-14T06:39:21.453269+00:00
2020-03-14T06:39:21.453302+00:00
537
false
```\nclass FizzBuzz {\n private int n;\n int curr;\n\n public FizzBuzz(int n) {\n this.n = n;\n curr = 1;\n }\n\n // printFizz.run() outputs "fizz".\n public synchronized void fizz(Runnable printFizz) throws InterruptedException {\n while(curr <= n) {\n if (curr % 5 == ...
2
0
[]
0
fizz-buzz-multithreaded
[Java] Use Semaphore
java-use-semaphore-by-yuhwu-sn4d
\nclass FizzBuzz {\n private int n;\n private int number;\n private Semaphore fizzS;\n private Semaphore buzzS;\n private Semaphore fizzBuzzS;\n
yuhwu
NORMAL
2020-02-04T07:34:59.993932+00:00
2020-02-04T07:34:59.993985+00:00
426
false
```\nclass FizzBuzz {\n private int n;\n private int number;\n private Semaphore fizzS;\n private Semaphore buzzS;\n private Semaphore fizzBuzzS;\n private Semaphore numberS;\n public FizzBuzz(int n) {\n this.n = n;\n number = 0;\n fizzS = new Semaphore(0);\n buzzS = new...
2
0
[]
2
fizz-buzz-multithreaded
Python 3 using one Barrier
python-3-using-one-barrier-by-kangtastic-3ezg
Maintain a counter as an instance attribute and synchronize execution of the four threads using threading.Barrier(). Barrier() takes a Callable during initializ
kangtastic
NORMAL
2019-12-11T05:15:51.730498+00:00
2019-12-11T05:49:55.007612+00:00
432
false
Maintain a counter as an instance attribute and synchronize execution of the four threads using `threading.Barrier()`. `Barrier()` takes a `Callable` during initialization that will mutate object state immediately before all the threads are released.\n```\nimport threading\n\n\nclass FizzBuzz:\n def __init__(self, n...
2
0
[]
0
fizz-buzz-multithreaded
Very simple: Python Barrier(), 94% / 100%
very-simple-python-barrier-94-100-by-zob-m2jf
We use a single primitive, threading.Barrier. Barrier(4) forces all four threads to count together: all threads pass the barrier if an only if all four threads
zobeebee
NORMAL
2019-11-30T20:47:09.642300+00:00
2019-11-30T21:11:44.588885+00:00
279
false
We use a single primitive, `threading.Barrier`. `Barrier(4)` forces all four threads to count together: all threads pass the barrier if an only if all four threads have reached it. There is one `Barrier` per integer we need to count. That\'s it.\n\n```\nfrom threading import Barrier\n\nclass FizzBuzz:\n def __init__...
2
0
[]
0
fizz-buzz-multithreaded
Python 99.17% time by cheating
python-9917-time-by-cheating-by-easytea-trub
Experimented with different python threading primitives, but performance were all consistently terrible. My guess is that underhanded solutions like these are s
easytea
NORMAL
2019-10-13T05:24:51.183498+00:00
2019-10-13T05:24:51.183544+00:00
434
false
Experimented with different python `threading` primitives, but performance were all consistently terrible. My guess is that underhanded solutions like these are skewing the runtime stats.\n\nCheating solution: store function references and run normal fizzbuzz once all functions are registered. Lock is still necessary t...
2
0
[]
1
fizz-buzz-multithreaded
Java simple AtomicInteger solution without locks.
java-simple-atomicinteger-solution-witho-vg81
\nclass FizzBuzz {\n private int n;\n AtomicInteger cur = new AtomicInteger(1);\n\n public FizzBuzz(int n) {\n this.n = n;\n }\n\n // prin
todor91
NORMAL
2019-10-07T12:09:13.683717+00:00
2019-10-07T12:09:13.683754+00:00
401
false
```\nclass FizzBuzz {\n private int n;\n AtomicInteger cur = new AtomicInteger(1);\n\n public FizzBuzz(int n) {\n this.n = n;\n }\n\n // printFizz.run() outputs "fizz".\n public void fizz(Runnable printFizz) throws InterruptedException {\n while (true) {\n int x = cur.get();\n ...
2
1
[]
2
fizz-buzz-multithreaded
C implementation with pthread
c-implementation-with-pthread-by-kamanel-lk03
\ntypedef enum Status_s{NUM, THR, FIV, THRFIV} Status;\ntypedef struct {\n int n;\n int cur;\n Status s;\n pthread_mutex_t m;\n pthread_cond_t th
kamanelf
NORMAL
2019-10-03T22:20:36.095244+00:00
2019-10-03T22:20:36.095285+00:00
587
false
```\ntypedef enum Status_s{NUM, THR, FIV, THRFIV} Status;\ntypedef struct {\n int n;\n int cur;\n Status s;\n pthread_mutex_t m;\n pthread_cond_t th;\n pthread_cond_t fv;\n pthread_cond_t thfv;\n pthread_cond_t num;\n} FizzBuzz;\n\nFizzBuzz* fizzBuzzCreate(int n) {\n FizzBuzz* obj = (FizzBuzz...
2
0
[]
0
fizz-buzz-multithreaded
Python solution using only threading.Lock (not Condition, Semaphore, or Event)
python-solution-using-only-threadinglock-18js
Uses threading.Lock\n\nNot very fast though. Got:\n\n runtime: 64 ms, faster than 27.89% of Python3 submissions\n memory usage: 16.4 MB, less than 100.00% of Py
butterscotchchip
NORMAL
2019-10-03T03:54:24.057162+00:00
2019-10-03T03:56:21.789062+00:00
302
false
Uses `threading.Lock`\n\nNot very fast though. Got:\n\n* runtime: 64 ms, faster than 27.89% of Python3 submissions\n* memory usage: 16.4 MB, less than 100.00% of Python3 submissions\n\n```python\nfrom threading import Lock\n\nclass FizzBuzz:\n def __init__(self, n: int):\n self.n = n\n self.fizzLock = ...
2
0
[]
0
fizz-buzz-multithreaded
Python naive condition variable
python-naive-condition-variable-by-make_-gigc
~~~python\n\nfrom threading import Condition\nclass FizzBuzz:\n def init(self, n: int):\n self.n = n\n self.cur = 1\n self.cv = Conditio
make_up
NORMAL
2019-09-29T19:40:26.353323+00:00
2019-09-29T19:40:26.353381+00:00
408
false
~~~python\n\nfrom threading import Condition\nclass FizzBuzz:\n def __init__(self, n: int):\n self.n = n\n self.cur = 1\n self.cv = Condition()\n\n def next(self):\n self.cur+=1\n self.cv.notify_all()\n \n # printFizz() outputs "fizz"\n def fizz(self, printFizz: \'C...
2
0
[]
0