Leetcode 264. Ugly number II

Source: Internet
Author: User

Write a program to find the `n` -th ugly number.

Ugly numbers is positive numbers whose prime factors only include `2, 3, 5` . For example, is the sequence of the first `1, 2, 3, 4, 5, 6, 8, 9, 10, 12` `10` ugly numbers.

Note that's `1` typically treated as an ugly number.

Hint:

1. The naive approach is to call for `isUgly` every number until you reach the nth one. Most numbers is not ugly. Try to focus your effort in generating only the ugly ones.
2. An ugly number must is multiplied by either 2, 3, or 5 from a smaller ugly number.
3. The key is what to maintain the order of the ugly numbers. Try A similar approach of merging from three sorted lists:l1, L2, and L3.
4. Assume you have Uk, the kth ugly number. Then uk+1 must is Min (L1 * 2, L2 * 3, L3 * 5).

"Problem Analysis"

1. Number of ugly

After the ugly number is decomposed, only the 2,3,5,1 default to the first ugly number is included.

2. The title requires the first n ugly number

The hints given contain an important message: that the larger number of ugly is obtained by multiplying the smaller number of ugly by 2 or 3 or 5, and is incremented.

Ideas

Let's give a simple example to analyze

1. The initial first ugly number is 1. Let's ask for a second ugly number.

Calculation: 1*2,1*3,1*5 results in 2 is the smallest, so the second ugly number is 2

2. Calculate a third ugly number

2 multiply the number of existing ugly by one, and find the first number larger than 2--4

3 Multiply the number of existing ugly by one, and find the first number larger than 2--3

3 Multiply the number of existing ugly by one, and find the first number larger than 2--5

The smallest--3 in the result, so the third ugly number is 3

3. Calculate the fourth ugly number

2 multiply the number of existing ugly by one, and find the first number larger than 2--6

3 Multiply the number of existing ugly by one, and find the first number larger than 2--6

3 Multiply the number of existing ugly by one, and find the first number larger than 2--5

The smallest--5 in the result, so the third ugly number is 5

And so on

In this process we can maintain a pointer for each number of 2,3,5, pointing to the number that the last multiplication result is greater than the last ugly number. This avoids redundant computations.

"Java Code"

`1  Public classSolution {2      Public Static intNthuglynumber (intN) {3         int[] Uglynum =New int[N+1];4UGLYNUM[1] = 1;5 6         intPoint2 = 1, Point3 = 1, point5 = 1;7          for(inti = 2; I <= N; i++){8             intCpoint2, Cpoint3, cpoint5;9             intnum2 = 0, num3 = 0, NUM5 = 0;Ten              for(Cpoint2 = Point2; cpoint2 < i; cpoint2++) {//find the number multiplied by 2 for the first one greater than the current last ugly number One                 if(Uglynum[cpoint2]*2 > Uglynum[i-1]){ Anum2 = uglynum[cpoint2]*2; -                      Break; -                 } the             } -              for(Cpoint3 = Point3; cpoint3 < i; cpoint3++) {//find the number multiplied by 3 for the first one greater than the current last ugly number -                 if(Uglynum[cpoint3]*3 > Uglynum[i-1]){ -NUM3 = uglynum[cpoint3]*3; +                      Break; -                 } +             } A              for(cpoint5 = point5; cpoint5 < i; cpoint5++) {//find the number multiplied by 5 for the first one greater than the current last ugly number at                 if(Uglynum[cpoint5]*5 > Uglynum[i-1]){ -NUM5 = uglynum[cpoint5]*5; -                      Break; -                 } -             } -             if(num2 <= num3 && num2 <=NUM5) { inUglynum[i] =num2; -Point2 = cpoint2+1; toPoint3 =Cpoint3; +POINT5 =cpoint5; -             } the             Else if(num3 <= num2 && num3 <=NUM5) { *Uglynum[i] =num3; \$Point3 = cpoint3+1;Panax NotoginsengPoint2 =Cpoint2; -POINT5 =cpoint5; the             } +             Else{ AUglynum[i] =NUM5; thePOINT5 = cpoint5+1; +Point3 =Cpoint3; -Point2 =Cpoint2; \$             } \$         } -         returnUglynum[n]; -     } the}`

"Code Optimization"

`1  Public classSolution {2      Public Static intNthuglynumber (intN) {3         int[] Uglynum =New int[N+1];4UGLYNUM[1] = 1;5 6         intPointer2 = 1, Pointer3 = 1, Pointer5 = 1;7          for(inti = 2; I <= N; i++){8             intmin = Math.min (Math.min (uglynum[pointer2]*2, uglynum[pointer3]*3), uglynum[pointer5]*5);9             if(min = = uglynum[pointer2]*2) pointer2++;Ten             if(min = = uglynum[pointer3]*3) pointer3++; One             if(min = = uglynum[pointer5]*5) pointer5++; AUglynum[i] =min; -         } -         returnUglynum[n]; the     } -}`

In reference to other people's code, found that their ideas and other people's thinking is actually similar, but the gap in the code is so big! This not only manifests itself in the code, but also in the complexity of time!

Leetcode 264. Ugly number II

Related Keywords:

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.