diff --git a/264. Ugly Number II b/264. Ugly Number II new file mode 100644 index 0000000..86dccba --- /dev/null +++ b/264. Ugly Number II @@ -0,0 +1,36 @@ +class Solution { +public: + int nthUglyNumber(int n) { + vector arr2; + vector arr3; + vector arr5; + vector result; + if(n==1){ + return 1; + } + else{ + result.push_back(1); + arr2.push_back(2); + arr3.push_back(3); + arr5.push_back(5); + + int i=0; + int j=0; + int k=0; + + while (result.size() < n) { + int mini = min({arr2[i], arr3[j], arr5[k]}); + result.push_back(mini); + + if (mini == arr2[i]) i++; + if (mini == arr3[j]) j++; + if (mini == arr5[k]) k++; + + arr2.push_back(result.back() * 2); + arr3.push_back(result.back() * 3); + arr5.push_back(result.back() * 5); + } + return result.back(); + } + } +};