✅ C++ || 2 Approaches || Explained With Algorithm || Easy & Simple
1007. Minimum Domino Rotations For Equal Row\nKNOCKCAT\n\n\n1. Easy C++\n2. Line by Line Explanation with Comments.\n3. Detailed Explanation \u2705\n4. 2 approa
# 1007. Minimum Domino Rotations For Equal Row\n**KNOCKCAT**\n\n```\n1. Easy C++\n2. Line by Line Explanation with Comments.\n3. Detailed Explanation \u2705\n4. 2 approaches one using frequency array & other by maintaining swaps .\n5. Please Upvote if it helps\u2B06\uFE0F\n6. Link to my Github Profile contains a repository of Leetcode with all my Solutions. \u2B07\uFE0F\n\t// \uD83D\uDE09If you Like the repository don\'t foget to star & fork the repository\uD83D\uDE09\n```\n``` ```\n[LeetCode](http://github.com/knockcat/Leetcode) **LINK TO LEETCODE REPOSITORY**\n``` ```\n **FIRST APPROACH**\n* **ALGORITHM**\n\t* We will keep **faceA** for **count the occurences of numbers in tops.**\n\t* **faceB** for **count the occurences of n umbers in bottoms.**\n\t* **same** for **counting the same occurences** in **top & bottom.**\n\t* We wiil **try all possibilities of domino from 1 to 6.**\n\t* If we **can make number** *i* in a **whole row** it should **satisfy** that **faceA[i] + faceB[i] - same[i] == n**\n\t* eg the first test Case.\n\t\t*  **tops** = [**2,1,2,4,2,2**], **bottoms** = [**5,2,6,2,3,2**]\n\t\t*  **faceA[2] = 4** , as tops[0] = tops[2] = tops[4] = tops[5]\n\t\t*  **faceB[2] = 3**, as bottoms[1] = bottoms[3] = bottoms[5]\n\t\t*  **same[2] = 1**, as tops[5] = bottoms[5]\n\t\t*  **Therefore** we have f**aceA[2] + faceB[2] - same[2] = 6**, (size of arr)\n\t\t*  Therfore **we can make 2 in a whole row.**\n\nANALYSIS :-\n* **TIME COMPLEXITY :- O(n)**\n* **SPACE COMPLEXITY :- O(1)**\n``` ```\n\n**CODE WITH EXPLANATION**\n```\n\t\t\t// \uD83D\uDE09\uD83D\uDE09\uD83D\uDE09\uD83D\uDE09Please upvote if it helps \uD83D\uDE09\uD83D\uDE09\uD83D\uDE09\uD83D\uDE09\nclass Solution {\npublic:\n    int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {\n        int n = tops.size();\n        \n        // faceA for counting the occurence in tops\n\t\t// faceB for counting occurence of numbers in bottoms\n        // same for counting when both tops and bottoms have same occurences\n        vector<int> faceA(7), faceB(7), same(7);\n        \n        for(int i = 0; i < n; ++i)\n        {\n            ++faceA[tops[i]];\n            ++faceB[bottoms[i]];\n            \n            if(tops[i] == bottoms[i])\n                ++same[tops[i]];\n        }\n        \n        int minRotation = INT_MAX;\n        \n\t\t// all possibilities from 1 to 6\n        for(int i = 1; i<=6; ++i)\n        {\n            if(faceA[i] + faceB[i] - same[i] == n)\n                minRotation = min(minRotation , min(faceA[i],faceB[i]) - same[i]);\n        }\n        \n        return minRotation == INT_MAX ? -1 : minRotation;\n        \n    }\n};\n```\n\n* **APPROACH 2**\n\n**AlGORITHM**\n* As we **have to find minimum Number of Rotations** to make **both array same.**\n* we **try to make all tops faces same**, & **all bottoms faces same**.\n* we will **iterate for all 6 values** and **calculate the swap for all 6 values**, **by 3 conditions :-**\n* if **(tops[j] == i)** (means **no need to swap** therefore we will **continue**)\n* else if **(bottoms[j] == i)** (means **swap is possible**) we **increment the swap**.\n* **else swap is not possible** and neither values are same we **break the loop.**\n* As **iteration reaches the end** we check if **(minRotaion >  swap)** & **update minRotaion** as **we have to find minimum.**\n* we will **get the number of rotaion of making both tops faces same** & **bottoms faces same.**\n* now we have to **return min of both** , or if **(ab == -1)** then **ba** or if **(ba == -1)** then **ab**.\n``` ```\n\nANALYSIS :-\n* **TIME COMPLEXITY :- O(n)**\n* **SPACE COMPLEXITY :- O(1)**\n``` ```\n\n**CODE WITH EXPLANATION**\n```\n\t\t\t\t\t// \uD83D\uDE09\uD83D\uDE09\uD83D\uDE09\uD83D\uDE09Please upvote if it helps \uD83D\uDE09\uD83D\uDE09\uD83D\uDE09\uD83D\uDE09\nclass Solution {\npublic:\n    \n    int dominoRotations(vector<int>& tops, vector<int>& bottoms)\n    {\n        int minRotation = INT_MAX;\n        for(int i = 1; i <= 6; ++i)\n        {\n            int swap = 0 , j;\n            for(j = 0; j<tops.size(); ++j)\n                {\n                    if(tops[j] == i)\n                        continue;\n                    else if(bottoms[j] == i)\n                        ++swap;\n                    else\n                        break;\n                }\n                \n                // if we reach at the end of vector && minRotation > swap\n                if(j == tops.size() && minRotation > swap)\n                    minRotation = swap;\n        }\n        return minRotation == INT_MAX ? -1 : minRotation;\n    }\n    \n    int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {\n        \n        // try to make all tops faces same\n        int ab = dominoRotations(tops,bottoms);\n        \n        // try to make all tops faces same\n        int ba = dominoRotations(bottoms,tops);\n        \n        return ab == -1 ? ba : ba == -1 ? ab : min(ab,ba);\n    }\n};\n```\n\n
['C']