4 Possible Scenarios | Straightforward O(n) Solution
This is a Google OA 2nd problem I have met before. At time, I provided a very straightforward solution and it worked. \n\nTo Solve this problem, we can treat th
This is a **Google OA 2nd problem** I have met before. At time, I provided a very **straightforward solution** and it worked. \n\nTo Solve this problem, we can treat the problem statement as: there are 2 arrays for 2 starting element (A[0], B[0]), try to find the minimum swap times bwteen these 4 ( = 2 * 2) scenarios:\n**1. if we can make all elements from array A equal to A[0] by swap.\n2. if we can make all elements from array A equal to B[0] by swap.\n3. if we can make all elements from array B equal to A[0] by swap.\n4. if we can make all elements from array B equal to B[0] by swap.**\n\n**Last but not least, If there is one of four results >-1, we return that value as the final answer; if all 4 scenarios is -1, then we return -1.**\n\nHere is my Java solution. The helper(int startValue, int[] A, int[] B) is to find the minimum swap times of the given element.\n```\n    public int minDominoRotations(int[] A, int[] B) {\n        int res1 = helper(A[0], A, B);\n        int res2 = helper(B[0], A, B);\n        if (res1 == -1) return res2;\n        if (res2 == -1) return res1;\n        return Math.min(res1, res2);\n    }\n    \n    public int helper(int s, int[] A, int[] B) {\n        int res1 = 0, res2 = 0;\n        for (int i=0; i<A.length; i++) {\n            if (A[i] != s) {\n                if (B[i] != s) {\n                    res1 = -1;\n                    break;                    \n                } else \n                    res1++;\n            }\n        }\n        for (int i=0; i<A.length; i++) {\n            if (B[i] != s) {\n                if (A[i] != s) {\n                    res2 = -1;\n                    break;\n                } else \n                    res2++;\n            }\n        }\n        if (res1 == -1) return res2;\n        if (res2 == -1) return res1;\n        return Math.min(res1, res2);\n    }\n```
[]