O(1) DP C++ Soln with Explanation
So here we will use DP \n Lets take 3 pointers p2, p3 and p5 for the factors 2, 3 and 5 respectively.\n In a result vector res we will store the min((res[i]2), 
So here we will use DP \n Lets take 3 pointers p2, p3 and p5 for the factors 2, 3 and 5 respectively.\n In a result vector res we will store the min((res[i]*2), min(res[i]*3, res[i]*5))\n \n Example\n N=5\n \n p2 ,p3 and p5 all are at 1st index\n we have to calculate min(p2*2, p3*3, p5*5)\n \n now for i=1......min(1*2 , 1*3, 1*5) is 2\n so p2++;\n res = (1,2)\n \n now p2 is at 2nd index and p3 and p5 still at 1st..\n for i=2.........min(2*2, 1*3, 1*5) is 3\n so p3++;\n res = (1,2,3)\n \n Now p2 and p3 are at 2nd index and p5 still at 1st\n for i=3......min(2*2, 2*3, 1*5) is 4\n so p2++\n res= (1, 2, 3, 4)\n \n now p2 is at 3rd index, p3 at 2nd and p5 at 1st\n for i=4........min(3*2, 2*3, 1*5) is 5\n so p5++;\n res=(1, 2, ,3 ,4 ,5)\n \n**hence nth term is 5**\n\n```\nclass Solution{\npublic:\n\t\tint nthUglyNumber(int n) {\n\t\t\t\n\t\t\tif(n==1)\n\t\t\t\treturn true;\n\t\t\t\n\t\t\tvector<int> res(n);\n\t\t\tint p1=0, p2=0, p5=0;\n\t\t\tres[0]=1;\n\t\t\t\n\t\t\tfor(int i=1; i<n; i++)\n\t\t\t{\n\t\t\t\t\tres[i]= min((min(res[p2]*2), min(res[p3]*3, res[p5]*5));\n\n\t\t\t\t\tif(res[i] == res[p2]*2)\n\t\t\t\t\t\tp2++;\n\n\t\t\t\t\tif(res[i] == res[p3]*3)\n\t\t\t\t\t\tp3++;\n\n\t\t\t\t\tif(res[i] == res[p5]*5)\n\t\t\t\t\t\tp5++;\n\t\t\t}\n\t\t\treturn res[n-1];\n\t\t}\n};\n```\n\n\n \n
[]