Yes, I will provide a video with a brief explanation and upload it . This approach does not work for Ill because in II 2, 3, and 5 are prime numbers and not divisible by any other numbers. However, in Ill, a, b, and c can be any numbers. For example, consider 10 with a = 2, b = 11, and c = 13. 10 is divisible by 2, but pow(2,x) != 10, so we cannot represent 10 in the form 2^a *11^b* 13^c. Therefore, 10 is considered an "ugly" number because it is divisible by 2.
Code :-
class Solution {
public:
int nthUglyNumber(int n) {
vector results (1,1);
int i = 0, j = 0, k = 0;
while (results.size() < n)
{
results.push_back(min(results[i] * 2, min(results[j] * 3, results[k] * 5)));
if (results.back() == results[i] * 2) ++i;
if (results.back() == results[j] * 3) ++j;
if (results.back() == results[k] * 5) ++k;
}
return results.back();
}
};
cool approach , thanks for sharing. Gd explanation.
Thank You ❤️
bhai ...ek number ....soo good solution
Thank you ❤️
Please do this everyday for at least 3 months I'll watch everyday
Sure, I will do .. ❤️✌️
good explaination
Thank You ❤️
can you help to provide the solution of ugly number III ?
why this approach failed in III
Yes, I will provide a video with a brief explanation and upload it .
This approach does not work for Ill because in II 2, 3, and 5 are prime numbers and not divisible by any other numbers.
However, in Ill, a, b, and c can be any numbers.
For example, consider 10 with a = 2, b = 11, and c = 13. 10 is divisible by 2, but pow(2,x) != 10, so we cannot represent 10 in the form 2^a *11^b* 13^c.
Therefore, 10 is considered an "ugly" number because it is divisible by 2.
i did this question after 2.5 hours hard analysis 😓😓
No problem. Learn it, and remember this concept next time. ✌️