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, fronts[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
    }
}

@@ -529,21 +529,20 @@
 result, 
-front
+back
 s%5Bi%5D);%0A 
@@ -983,16 +983,18 @@
 m822();%0A
+//
         
@@ -1077,32 +1077,34 @@
 , 1, 3%7D)); // 2%0A
+//
         System.o
@@ -1155,32 +1155,34 @@
 nt%5B%5D%7B3%7D)); // 2%0A
+//
         System.o
@@ -1233,32 +1233,34 @@
 nt%5B%5D%7B2%7D)); // 2%0A
+//
         System.o
@@ -1319,16 +1319,18 @@
 ); // 0%0A
+//
         

Update problem 822