LeetCode 1115. Print FooBar Alternately

原题链接在这里:https://leetcode.com/problems/print-foobar-alternately/

题目:

Suppose you are given the following code:

class FooBar {
  public void foo() {
    for (int i = 0; i < n; i++) {
      print("foo");
    }
  }

  public void bar() {
    for (int i = 0; i < n; i++) {
      print("bar");
    }
  }
}

The same instance of FooBar will be passed to two different threads:

  • thread A will call foo(), while
  • thread B will call bar().

Modify the given program to output "foobar" n times.

Example 1:

Input: n = 1
Output: "foobar"
Explanation: There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar().
"foobar" is being output 1 time.

Example 2:

Input: n = 2
Output: "foobarfoobar"
Explanation: "foobar" is being output 2 times. 

Constraints:

  • 1 <= n <= 1000

题解:

We could use Semaphore. One Semaphore for foo has count 1, then first acquire() don't need to wait. 

The other Semaphore for bar has count 0, then first acquire() needs to wait.

Time Complexity: O(1).

Space: O(1).

AC Java:

 1 class FooBar {
 2     private int n;
 3     private Semaphore s0;
 4     private Semaphore s1;
 5     
 6     public FooBar(int n) {
 7         this.n = n;
 8         this.s0 = new Semaphore(0);
 9         this.s1 = new Semaphore(1);
10     }
11 
12     public void foo(Runnable printFoo) throws InterruptedException {
13         
14         for (int i = 0; i < n; i++) {
15             s1.acquire();
16             
17             // printFoo.run() outputs "foo". Do not change or remove this line.
18             printFoo.run();
19             s0.release();
20         }
21     }
22 
23     public void bar(Runnable printBar) throws InterruptedException {
24         
25         for (int i = 0; i < n; i++) {
26             s0.acquire();
27             
28             // printBar.run() outputs "bar". Do not change or remove this line.
29             printBar.run();
30             s1.release();
31         }
32     }
33 }

Could also use synchronized.

Time Complexity: O(1).

Space: O(1).

AC Java:

 1 class FooBar {
 2     private int n;
 3     private boolean fooRunning;
 4     
 5     public FooBar(int n) {
 6         this.n = n;
 7         this.fooRunning = false;
 8     }
 9 
10     public void foo(Runnable printFoo) throws InterruptedException {
11         
12         for (int i = 0; i < n; i++) {
13             synchronized(this){
14                 while(fooRunning){
15                     this.wait();
16                 }
17                 
18                 // printFoo.run() outputs "foo". Do not change or remove this line.
19                 printFoo.run();
20                 fooRunning = true;
21                 this.notifyAll();
22             }
23             
24         }
25     }
26 
27     public void bar(Runnable printBar) throws InterruptedException {
28         
29         for (int i = 0; i < n; i++) {
30             synchronized(this){
31                 while(!fooRunning){
32                     this.wait();
33                 }
34                 
35                 // printBar.run() outputs "bar". Do not change or remove this line.
36                 printBar.run();
37                 fooRunning = false;
38                 this.notifyAll();
39             }
40         }
41     }
42 }

 

posted @ 2022-10-14 12:22  Dylan_Java_NYC  阅读(37)  评论(0编辑  收藏  举报