Please check my code. I am getting TLE on submitting.
Ugly Numbers - Time Limit Error
If we have k ugly numbers already, then the k+1 ugly number has to be some multiple of the previous ugly numbers only else it would not remain ugly.
We need to multiply previous ugly numbers by 2,3 and 5 and choose the next highest.
But if we multiply all the previous numbers by 2,3 and 5 then it will become thrice O(n2) operations to find all such multiples and then another O(n) operations to find the minimum among them.
To optimize, we will keep track where last multiple of each (2,3,5) was kept.
If we know that, then we need not consider ugly numbers previous to those.dont over complicate
you can see this