Nth number of ugly

Source: Internet
Author: User

I think for two days have no idea ah ah ah ah oh I was so stupid. Read his tag, said the use of dynamic planning, so deliberately read the introduction of the algorithm in the dynamic planning part. However, the first step is to construct a reasonable data structure, and the second is to solve it in the form of recursion. Visible water impermanence, dynamic planning is not a simple formula can be solved. So how do you deal with it?

The first is that when dynamic planning is used, it is the case that the results from the previous results are then stored after the results have been solved. The former implies its recursive structure, which has the meaning of space-changing time.

Clear this goes into the second question, what is the ugly number?

In addition to 1, the number consisting of only 2 3 53 vegetarian Factors is the number of ugly. Since the vegetarian factor can only have 2 3 5, then the number of ugly in the back must be obtained by multiplying the number of 2/3/5 in front.

The next number is calculated from the front, and the previous ugly number is calculated and stored in the array, which is the two points mentioned earlier recursion and space change time.

So how is the new ugly number likely to be formed? Three kinds: 2 *A/3 * B/5 * C Therefore, three markers are set, indicating the position of a, B, and C respectively.

Among them, the smallest number becomes the new ugly number. And after it is selected as the new ugly number, the next new ugly number must be larger than this number (nonsense) so the corresponding tag is shifted back to ensure that the next new ugly number of three choices are greater than the current maximum number of ugly.

With the above instructions, you can understand the algorithm very well:

intNthuglynumber (intN) {if(N <1){        return 0; } Vector<int> ugliness{1}; Auto Index2=0; Auto Index3=0; Auto Index5=0; Auto Savenewuglyincreaseit= [&] {AutoConstVal2 = ugliness[index2] *2; AutoConstVAL3 = ugliness[index3] *3; AutoConstVAL5 = ugliness[index5] *5; AutoConstnewugly =min (val2, min (Val3, VAL5)); if(newugly = =val2) {            ++Index2; }        if(newugly = =Val3) {            ++index3; }        if(newugly = =VAL5) {            ++index5;    } ugliness.push_back (newugly);        };  for(inti =1; I! = N; ++i)    {Savenewuglyincreaseit (); }    returnugliness.back ();}

Nth number of ugly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.