Roll Dies for equal rows - Java - Easy Understanding - DP approach
Hi,\n\nThought process to solve this problem in constant time and space, So I went with the DP approach. \n\nHere we have only 6 values in the dies:\n1. We have
Hi,\n\nThought process to solve this problem in constant time and space, So I went with the DP approach. \n\nHere we have only 6 values in the dies:\n1. We have a constant space of 6 and need to refer whether it\'s from top or bottom, So go with DP[6][3] (one for top and the other for bottom)\n2. Next, we can iterate once and increment on 1 to 6\n3. Now, we will see if there is any value from 1 to 6 have a length value\n      a.If yes, then we can see the minimum of the swap\n      b.Else return -1 \n\t```\n\t    public int minDominoRotations(int[] tops, int[] bottoms) {\n        int len = tops.length;\n        int[][] dp = new int[6][3];\n        for(int i=0; i<len; i++){\n        \tif(tops[i]==bottoms[i]){\n        \t\tdp[tops[i]-1][0]++;\n        \t\tdp[tops[i]-1][1]++;\n        \t\tdp[tops[i]-1][2]++;\n        \t}else{\n        \t\tdp[tops[i]-1][0]++;\n        \t\tdp[tops[i]-1][1]++;\n        \t\tdp[bottoms[i]-1][0]++;\n        \t\tdp[bottoms[i]-1][2]++;\n        \t}\n        }\n        int min = Integer.MAX_VALUE;\n        for(int i=0; i<6; i++){\n        \tif(dp[i][0]==len){\n        \t\tmin = Math.min(min, Math.min(len-dp[i][1], len-dp[i][2]));\n        \t}\n        }\n        if(min<len)\n        \treturn min;\n        return -1;\n    }\n\t```\n\tDo upvote if it helps.. Happy learning!! :)
['Dynamic Programming', 'Java']