Python faster than 90% with explanation o(n)
Hello guys, I want to share my thoughts and solutions. If  I make any mistakes or do something wrong, please feel free to let me know. I will appreciate it : )\
Hello guys, I want to share my thoughts and solutions. If  I make any mistakes or do something wrong, please feel free to let me know. I will appreciate it : )\n<br>\n\nFollow the description, after rotations the value in `A` or `B` need to be the same. So, every index in `A` or `B` sould have one same value. So, I want to find the value first. The value should satisfy two conditions as follows\uFF1A\n\n(1) count number of the value should be the most (one of list has all this value in all index,so number should `>=len(A)`)\n(2) the value must be in first index of `A` or `B` (in python means `A[0]` and `B[0]`)\n\t\nUsing `count` to find the most value and set it as `temp`:\n\n```\n\tif A.count(A[0])+B.count(A[0]) >= len(A):\n\t\ttemp=A[0]\n\telse:\n\t\ttemp=B[0]\n```\n<br>\n\nSetup two variable, `ans` means the number of rotation and `same` means value in `A` and `B` are same, do not need rotate.\n\n```\nans=0\nsame=0\n```\n<br>\n\nFind the `A` value not equal to `temp`, and rotate:  `ans+=1`.\nWhen both `A` and `B` are `temp` in same index, do not need rotate:`same+=1`.\nWhen `temp` not in this index, return `-1`.\n\n```\nfor i in range(len(A)):\n\tif temp ==B[i] or temp==A[i]:\n\t\tif temp ==B[i] and temp!=A[i]:\n\t\t\tans+=1\n\t\telif temp ==B[i] and temp==A[i]:\n\t\t\tsame+=1\n\telse:\n\t\treturn -1\n```\n<br>\n\nIn final, `ans` means number of rotate `temp` into `A`. It also need to know who many number of rotate `temp` into `B`, the number is `len(A)-ans-same`.\nUsing `min` to find the less one and return.\n\n```\nreturn min(len(A)-ans-same,ans)\n```\n<br>\nThe complete answer is as follows:\n\n```\nclass Solution:\n    def minDominoRotations(self, A: List[int], B: List[int]) -> int:\n        \n        if A.count(A[0])+B.count(A[0]) >= len(A):\n            temp=A[0]\n        else:\n            temp=B[0]\n            \n        ans=0\n        same=0\n\t\t\n        for i in range(len(A)):\n            if temp ==B[i] or temp==A[i]:\n                if temp ==B[i] and temp!=A[i]:\n                    ans+=1\n                elif temp ==B[i] and temp==A[i]:\n                    same+=1\n            else:\n                return -1\n        return min(len(A)-ans-same,ans)\n```\n\nThank you for your attention : )
[]