package leetcode;

/**
 * https://leetcode.com/problems/card-flipping-game/
 */
public class Problem822 {
    public int flipgame(int[] fronts, int[] backs) {
        int[] set = new int[2000];
        int result = Integer.MAX_VALUE;
        for (int i = 0; i < fronts.length; i++) {
            set[fronts[i]]++;
        }
        for (int i = 0; i < fronts.length; i++) {
            if (fronts[i] == backs[i]) {
                continue;
            }
            if (set[fronts[i]] == 1) {
                result = Math.min(result, backs[i]);
            }
            // Flip
            set[backs[i]]++;
            set[fronts[i]]--;
            if (set[backs[i]] == 1) {
                result = Math.min(result, backs[i]);
            }
            // Restore
            set[backs[i]]--;
            set[fronts[i]]++;
        }
        return result == Integer.MAX_VALUE ? 0 : result;
    }

    public static void main(String[] args) {
        Problem822 prob = new Problem822();
//        System.out.println(prob.flipgame(new int[]{1, 2, 4, 4, 7}, new int[]{1, 3, 4, 1, 3})); // 2
//        System.out.println(prob.flipgame(new int[]{2}, new int[]{3})); // 2
//        System.out.println(prob.flipgame(new int[]{3}, new int[]{2})); // 2
//        System.out.println(prob.flipgame(new int[]{3}, new int[]{3})); // 0
//        System.out.println(prob.flipgame(new int[]{1, 1}, new int[]{1, 2})); // 2
        System.out.println(prob.flipgame(new int[]{1, 1}, new int[]{2, 2})); // 1
    }
}

@@ -160,19 +160,23 @@
         
-int
+boolean
 %5B%5D set =
@@ -180,19 +180,23 @@
 t = new 
-int
+boolean
 %5B2000%5D;%0A
@@ -289,36 +289,36 @@
 ) %7B%0A            
-set%5B
+if (
 fronts%5Bi%5D%5D++;%0A  
@@ -302,36 +302,47 @@
    if (fronts%5Bi%5D
-%5D++;
+ == backs%5Bi%5D) %7B
 %0A        %7D%0A     
@@ -334,26 +334,24 @@
         
-%7D%0A
         
 for (int
@@ -346,90 +346,44 @@
     
-for (int i = 0; i %3C fronts.length; i++) %7B%0A            if (fronts%5Bi%5D == backs%5Bi%5D) %7B
+set%5Bfronts%5Bi%5D%5D = true;%0A            %7D
 %0A   
@@ -387,24 +387,26 @@
         
+%7D%0A
         
 continue
@@ -401,31 +401,49 @@
     
-continue;%0A            %7D
+for (int i = 0; i %3C fronts.length; i++) %7B
 %0A   
@@ -451,24 +451,25 @@
         if (
+!
 set%5Bfronts%5Bi
@@ -466,29 +466,24 @@
 t%5Bfronts%5Bi%5D%5D
- == 1
 ) %7B%0A        
@@ -508,36 +508,37 @@
 ath.min(result, 
-back
+front
 s%5Bi%5D);%0A         
@@ -566,67 +566,8 @@
 lip%0A
-            set%5Bbacks%5Bi%5D%5D++;%0A            set%5Bfronts%5Bi%5D%5D--;%0A
     
@@ -578,16 +578,17 @@
     if (
+!
 set%5Bback
@@ -596,13 +596,8 @@
 %5Bi%5D%5D
- == 1
 ) %7B%0A
@@ -675,746 +675,66 @@
     
-    // Restore%0A            set%5Bbacks%5Bi%5D%5D--;%0A            set%5Bfronts%5Bi%5D%5D++;%0A        %7D%0A        return result == Integer.MAX_VALUE ? 0 : result;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem822 prob = new Problem822();%0A//        System.out.println(prob.flipgame(new int%5B%5D%7B1, 2, 4, 4, 7%7D, new int%5B%5D%7B1, 3, 4, 1, 3%7D)); // 2%0A//        System.out.println(prob.flipgame(new int%5B%5D%7B2%7D, new int%5B%5D%7B3%7D)); // 2%0A//        System.out.println(prob.flipgame(new int%5B%5D%7B3%7D, new int%5B%5D%7B2%7D)); // 2%0A//        System.out.println(prob.flipgame(new int%5B%5D%7B3%7D, new int%5B%5D%7B3%7D)); // 0%0A//        System.out.println(prob.flipgame(new int%5B%5D%7B1, 1%7D, new int%5B%5D%7B1, 2%7D)); // 2%0A        System.out.println(prob.flipgame(new int%5B%5D%7B1, 1%7D, new int%5B%5D%7B2, 2%7D)); // 1
+%7D%0A        return result == Integer.MAX_VALUE ? 0 : result;
 %0A   

Solve problem 822