beats 100%
\n Describe your first thoughts on how to solve this problem. \n\n\n\n# Complexity\n- Time complexity:O(n)\n Add your time complexity here, e.g. O(n) \n\n- Spac
\n<!-- Describe your first thoughts on how to solve this problem. -->\n![image.png](https://assets.leetcode.com/users/images/a88985c1-bf75-47da-b117-eff15d7b2d8b_1727965205.4652915.png)\n\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    private int helper(int[] tops, int[] bottoms, int val) {\n        int top_res = 0, bottom_res = 0;\n        for (int i = 0; i < tops.length; i++) {\n            if (tops[i] != val && bottoms[i] != val) {\n                return -1;\n            } else if (tops[i] != val) {\n                top_res++;\n            } else if (bottoms[i] != val) {\n                bottom_res++;\n            }\n        }\n        return Math.min(top_res, bottom_res);\n    }\n\n\n\n    public int minDominoRotations(int[] tops, int[] bottoms) {\n        \n        \n        int ans = -1;\n        for (int i = 1; i < 7; i++) {\n            int cur_ans = helper(tops, bottoms, i);\n\n            if (cur_ans != -1 && (ans == -1 || ans > cur_ans)) {\n                ans = cur_ans;\n            }\n        }\n        return ans;\n    }\n}\n```
['Java']