Skip to content

Commit 64744cf

Browse files
committed
feat: Fizz-Buzz multithreaded
1 parent 371956d commit 64744cf

File tree

3 files changed

+275
-0
lines changed

3 files changed

+275
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
title: 1195. Fizz Buzz Multithreaded
3+
subtitle: "https://leetcode.com/problems/fizz-buzz-multithreaded/description/"
4+
date: 2024-02-18T16:25:00+08:00
5+
lastmod: 2024-02-18T16:25:00+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "1195.Fizz-Buzz-Multithreaded"
10+
license: ""
11+
images: []
12+
13+
tags: [LeetCode, Go, Medium, Fizz Buzz Multithreaded, Concurrency]
14+
categories: [LeetCode]
15+
16+
featuredImage: ""
17+
featuredImagePreview: ""
18+
19+
hiddenFromHomePage: false
20+
hiddenFromSearch: false
21+
twemoji: false
22+
lightgallery: true
23+
ruby: true
24+
fraction: true
25+
fontawesome: true
26+
linkToMarkdown: false
27+
rssFullText: false
28+
29+
toc:
30+
enable: true
31+
auto: true
32+
code:
33+
copy: true
34+
maxShownLines: 200
35+
math:
36+
enable: false
37+
# ...
38+
mapbox:
39+
# ...
40+
share:
41+
enable: true
42+
# ...
43+
comment:
44+
enable: true
45+
# ...
46+
library:
47+
css:
48+
# someCSS = "some.css"
49+
# located in "assets/"
50+
# Or
51+
# someCSS = "https://cdn.example.com/some.css"
52+
js:
53+
# someJS = "some.js"
54+
# located in "assets/"
55+
# Or
56+
# someJS = "https://cdn.example.com/some.js"
57+
seo:
58+
images: []
59+
# ...
60+
---
61+
# [1195. Fizz Buzz Multithreaded](https://leetcode.com/problems/fizz-buzz-multithreaded/description/)
62+
63+
## 題目
64+
You have the four functions:
65+
66+
printFizz that prints the word "fizz" to the console,
67+
printBuzz that prints the word "buzz" to the console,
68+
printFizzBuzz that prints the word "fizzbuzz" to the console, and
69+
printNumber that prints a given integer to the console.
70+
You are given an instance of the class FizzBuzz that has four functions: fizz, buzz, fizzbuzz and number. The same instance of FizzBuzz will be passed to four different threads:
71+
72+
Thread A: calls fizz() that should output the word "fizz".
73+
Thread B: calls buzz() that should output the word "buzz".
74+
Thread C: calls fizzbuzz() that should output the word "fizzbuzz".
75+
Thread D: calls number() that should only output the integers.
76+
Modify the given class to output the series [1, 2, "fizz", 4, "buzz", ...] where the ith token (1-indexed) of the series is:
77+
78+
"fizzbuzz" if i is divisible by 3 and 5,
79+
"fizz" if i is divisible by 3 and not 5,
80+
"buzz" if i is divisible by 5 and not 3, or
81+
i if i is not divisible by 3 or 5.
82+
Implement the FizzBuzz class:
83+
84+
FizzBuzz(int n) Initializes the object with the number n that represents the length of the sequence that should be printed.
85+
void fizz(printFizz) Calls printFizz to output "fizz".
86+
void buzz(printBuzz) Calls printBuzz to output "buzz".
87+
void fizzbuzz(printFizzBuzz) Calls printFizzBuzz to output "fizzbuzz".
88+
void number(printNumber) Calls printnumber to output the numbers.
89+
90+
91+
Example 1:
92+
93+
Input: n = 15
94+
Output: [1,2,"fizz",4,"buzz","fizz",7,8,"fizz","buzz",11,"fizz",13,14,"fizzbuzz"]
95+
Example 2:
96+
97+
Input: n = 5
98+
Output: [1,2,"fizz",4,"buzz"]
99+
100+
101+
Constraints:
102+
103+
1 <= n <= 50
104+
## 題目大意
105+
106+
107+
## 解題思路
108+
109+
## Big O
110+
時間複雜 : ``
111+
空間複雜 : ``
112+
113+
## 來源
114+
* https://leetcode.com/problems/fizz-buzz-multithreaded/description/
115+
* https://leetcode.cn/problems/fizz-buzz-multithreaded/description/
116+
117+
## 解答
118+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/1195.Fizz-Buzz-Multithreaded/main.go
119+
120+
```go
121+
122+
```
123+
124+
## Benchmark
125+
126+
```sh
127+
128+
```
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package fizzbuzzmultithreaded
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
)
7+
8+
var (
9+
wg = sync.WaitGroup{}
10+
)
11+
12+
// 時間複雜 O(), 空間複雜 O()
13+
func FizzBuzz(n int) {
14+
fb := NewFizzBuzz()
15+
16+
wg.Add(4)
17+
18+
go fb.fizz()
19+
go fb.buzz()
20+
go fb.fizzbuzz()
21+
go fb.number()
22+
23+
for i := 1; i <= n; i++ {
24+
if i%3 == 0 && i%5 == 0 {
25+
fb.fizzbuzzCh <- struct{}{}
26+
} else if i%3 == 0 {
27+
fb.fizzCh <- struct{}{}
28+
} else if i%5 == 0 {
29+
fb.buzzCh <- struct{}{}
30+
} else {
31+
fb.numberCh <- i
32+
}
33+
<-fb.orderCh
34+
}
35+
fb.done <- struct{}{}
36+
fb.done <- struct{}{}
37+
fb.done <- struct{}{}
38+
fb.done <- struct{}{}
39+
wg.Wait()
40+
}
41+
42+
type FizzBuzzStruct struct {
43+
numberCh chan int
44+
fizzCh chan struct{}
45+
buzzCh chan struct{}
46+
fizzbuzzCh chan struct{}
47+
orderCh chan struct{}
48+
done chan struct{}
49+
}
50+
51+
func NewFizzBuzz() *FizzBuzzStruct {
52+
return &FizzBuzzStruct{
53+
numberCh: make(chan int),
54+
fizzCh: make(chan struct{}),
55+
buzzCh: make(chan struct{}),
56+
fizzbuzzCh: make(chan struct{}),
57+
orderCh: make(chan struct{}),
58+
done: make(chan struct{}, 4),
59+
}
60+
}
61+
62+
func (fb *FizzBuzzStruct) fizz() {
63+
defer wg.Done()
64+
for {
65+
select {
66+
case <-fb.fizzCh:
67+
fmt.Print("fizz")
68+
fb.orderCh <- struct{}{}
69+
case <-fb.done:
70+
return
71+
}
72+
}
73+
}
74+
75+
func (fb *FizzBuzzStruct) buzz() {
76+
defer wg.Done()
77+
for {
78+
select {
79+
case <-fb.buzzCh:
80+
fmt.Print("buzz")
81+
fb.orderCh <- struct{}{}
82+
case <-fb.done:
83+
return
84+
}
85+
}
86+
}
87+
88+
func (fb *FizzBuzzStruct) fizzbuzz() {
89+
defer wg.Done()
90+
for {
91+
select {
92+
case <-fb.fizzbuzzCh:
93+
fmt.Print("fizzbuzz")
94+
fb.orderCh <- struct{}{}
95+
case <-fb.done:
96+
return
97+
}
98+
}
99+
}
100+
101+
func (fb *FizzBuzzStruct) number() {
102+
defer wg.Done()
103+
for {
104+
select {
105+
case v := <-fb.numberCh:
106+
fmt.Print(v)
107+
fb.orderCh <- struct{}{}
108+
case <-fb.done:
109+
return
110+
}
111+
}
112+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package fizzbuzzmultithreaded
2+
3+
import "testing"
4+
5+
var tests = []struct {
6+
arg1 int
7+
want []interface{}
8+
}{
9+
{
10+
15,
11+
[]interface{}{1, 2, "fizz", 4, "buzz", "fizz", 7, 8, "fizz", "buzz", 11, "fizz", 13, 14, "fizzbuzz"},
12+
},
13+
}
14+
15+
func TestFizzBuzz(t *testing.T) {
16+
for _, tt := range tests {
17+
// if got := ReverseList(tt.arg1); !reflect.DeepEqual(got, tt.want) {
18+
// if got := FizzBuzz(tt.arg1); got != tt.want {
19+
// t.Errorf("got = %v, want = %v", got, tt.want)
20+
// }
21+
FizzBuzz(tt.arg1)
22+
}
23+
}
24+
25+
func BenchmarkFizzBuzz(b *testing.B) {
26+
b.ResetTimer()
27+
for i := 0; i < b.N; i++ {
28+
FizzBuzz(tests[0].arg1)
29+
}
30+
}
31+
32+
/*
33+
go test -benchmem -run=none LeetcodeGolang/Leetcode/0354.Russian-Doll-Envelopes -bench=.
34+
35+
*/

0 commit comments

Comments
 (0)