package leetcode;

/**
 * https://leetcode.com/problems/bulb-switcher-iii/
 */
public class Problem1375 {
    public int numTimesAllBlue(int[] light) {
        int answer = 0;
        Color[] colors = new Color[light.length];
        for (int i = 0; i < light.length; i++) {
            colors[i] = Color.WHITE;
        }
        int yellowCount = 0;
        for (int l : light) {
            if (l - 2 >= 0 && colors[l - 2] == Color.BLUE && yellowCount == 0) {
                colors[l - 1] = Color.BLUE;
                answer++;
                continue;
            } else {
                colors[l - 1] = Color.YELLOW;
                yellowCount++;
            }
            int i = 0;
            while (i < light.length && colors[i].on) {
                if (colors[i] == Color.YELLOW) {
                    yellowCount--;
                }
                colors[i] = Color.BLUE;
                i++;
            }
            if (yellowCount == 0) {
                answer++;
            }
        }
        return answer;
    }

    private enum Color {
        WHITE(false),
        YELLOW(true),
        BLUE(true);

        private final boolean on;

        Color(boolean on) {
            this.on = on;
        }
    }

    public static void main(String[] args) {
        Problem1375 prob = new Problem1375();
//        System.out.println(prob.numTimesAllBlue(new int[]{2,1,3,5,4})); // 3
//        System.out.println(prob.numTimesAllBlue(new int[]{3,2,4,1,5})); // 2
        System.out.println(prob.numTimesAllBlue(new int[]{4,1,2,3})); // 1
//        System.out.println(prob.numTimesAllBlue(new int[]{2,1,4,3,6,5})); // 3
//        System.out.println(prob.numTimesAllBlue(new int[]{1,2,3,4,5,6})); // 6
//        System.out.println(prob.numTimesAllBlue(new int[]{3,2,1})); // 1
//        System.out.println(prob.numTimesAllBlue(new int[]{3,1,2})); // 1
    }
}

@@ -177,21 +177,19 @@
         
-Color
+int
 %5B%5D color
@@ -196,21 +196,19 @@
 s = new 
-Color
+int
 %5Blight.l
@@ -218,104 +218,43 @@
 th%5D;
-%0A        for (int i = 0; i %3C light.length; i++) %7B%0A            colors%5Bi%5D = Color.WHITE;%0A        %7D
+ // 0 = white, 1 = yellow, 2 = blue
 %0A   
@@ -356,26 +356,17 @@
 - 2%5D == 
-Color.BLUE
+2
  && yell
@@ -409,34 +409,25 @@
 rs%5Bl - 1%5D = 
-Color.BLUE
+2
 ;%0A          
@@ -521,28 +521,17 @@
  - 1%5D = 
-Color.YELLOW
+1
 ;%0A      
@@ -627,24 +627,25 @@
 t.length && 
+(
 colors%5Bi%5D.on
@@ -641,19 +641,40 @@
 olors%5Bi%5D
-.on
+ == 1 %7C%7C colors%5Bi%5D == 2)
 ) %7B%0A    
@@ -706,20 +706,9 @@
  == 
-Color.YELLOW
+1
 ) %7B%0A
@@ -792,18 +792,9 @@
 %5D = 
-Color.BLUE
+2
 ;%0A  
@@ -944,846 +944,7 @@
    %7D
-%0A%0A    private enum Color %7B%0A        WHITE(false),%0A        YELLOW(true),%0A        BLUE(true);%0A%0A        private final boolean on;%0A%0A        Color(boolean on) %7B%0A            this.on = on;%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1375 prob = new Problem1375();%0A//        System.out.println(prob.numTimesAllBlue(new int%5B%5D%7B2,1,3,5,4%7D)); // 3%0A//        System.out.println(prob.numTimesAllBlue(new int%5B%5D%7B3,2,4,1,5%7D)); // 2%0A        System.out.println(prob.numTimesAllBlue(new int%5B%5D%7B4,1,2,3%7D)); // 1%0A//        System.out.println(prob.numTimesAllBlue(new int%5B%5D%7B2,1,4,3,6,5%7D)); // 3%0A//        System.out.println(prob.numTimesAllBlue(new int%5B%5D%7B1,2,3,4,5,6%7D)); // 6%0A//        System.out.println(prob.numTimesAllBlue(new int%5B%5D%7B3,2,1%7D)); // 1%0A//        System.out.println(prob.numTimesAllBlue(new int%5B%5D%7B3,1,2%7D)); // 1%0A    %7D
 %0A%7D%0A

Solve problem 1375