package leetcode;

/**
 * https://leetcode.com/problems/print-foobar-alternately/
 */
public class Problem1115 {
    private static class FooBar {
        private int n;

        public FooBar(int n) {
            this.n = n;
        }

        public void foo(Runnable printFoo) throws InterruptedException {

            for (int i = 0; i < n; i++) {

                // printFoo.run() outputs "foo". Do not change or remove this line.
                printFoo.run();
            }
        }

        public void bar(Runnable printBar) throws InterruptedException {

            for (int i = 0; i < n; i++) {

                // printBar.run() outputs "bar". Do not change or remove this line.
                printBar.run();
            }
        }
    }
}

@@ -12,16 +12,56 @@
 tcode;%0A%0A
+import java.util.concurrent.Semaphore;%0A%0A
 /**%0A * h
@@ -202,16 +202,146 @@
 e int n;
+%0A        private final Semaphore fooSemaphore = new Semaphore(0);%0A        private final Semaphore barSemaphore = new Semaphore(2);
 %0A%0A      
@@ -465,33 +465,32 @@
 ptedException %7B%0A
-%0A
             for 
@@ -507,33 +507,72 @@
 ; i %3C n; i++) %7B%0A
+                barSemaphore.acquire();
 %0A
-
                 
@@ -663,32 +663,72 @@
 printFoo.run();%0A
+                fooSemaphore.release();%0A
             %7D%0A  
@@ -809,17 +809,16 @@
 ption %7B%0A
-%0A
         
@@ -851,17 +851,56 @@
  i++) %7B%0A
+                fooSemaphore.acquire();
 %0A
-
         
@@ -967,32 +967,32 @@
 move this line.%0A
-
                 
@@ -999,32 +999,72 @@
 printBar.run();%0A
+                barSemaphore.release();%0A
             %7D%0A  

Solve problem 1115