Ugly number II

Source: Internet
Author: User

Topic:

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).

Links: http://leetcode.com/problems/ugly-number-ii/

Exercises

Maths problem again .... Everyone should study maths well. The main idea is to refer to the solution inside the discuss, it is said that the use of CC150 in the method. First n = 1 o'clock, the result is 1. Next, three queue<long> are maintained, and if you don't use a Long then n = 1600+ will overflow. Using the recursive relationship, we compare the peek () in the Q2,q3 and Q5 respectively, and then calculate the next few ugly number.

Time Complexity-o (n), Space compleixty-o (n)

 Public classSolution { Public intNthuglynumber (intN) {if(N < 1)            return0; Queue<Long> q2 =NewLinkedlist<>(); Queue<Long> q3 =NewLinkedlist<>(); Queue<Long> Q5 =NewLinkedlist<>(); Q2.add (2l); Q3.add (3l); Q5.add (5l); Long Res= 1l;  while(N > 1) {            if(Q2.peek () < Q3.peek () && Q2.peek () <Q5.peek ()) {Res=Q2.poll (); Q2.add (Res* 2); Q3.add (Res* 3); Q5.add (Res* 5); } Else if(Q3.peek () < Q2.peek () && Q3.peek () <Q5.peek ()) {Res=Q3.poll (); Q3.add (Res* 3); Q5.add (Res* 5); } Else{res=Q5.poll (); Q5.add (Res* 5); } N--; }                returnRes.intvalue (); }}

Reference:

http://www.geeksforgeeks.org/ugly-numbers/

Https://leetcode.com/discuss/52722/short-and-o-n-python-and-c

http://www.stefan-pochmann.info/spocc/

Https://leetcode.com/discuss/53009/interesting-bounds-about-this-problem

Https://leetcode.com/discuss/53505/using-three-queues-java-solution

Https://leetcode.com/discuss/55304/java-easy-understand-o-n-solution

Https://leetcode.com/discuss/55307/c-solution-with-o-n-time

Https://leetcode.com/discuss/57156/my-expressive-python-solution

Https://leetcode.com/discuss/58186/elegant-c-solution-o-n-space-time-with-detailed-explanation

Https://leetcode.com/discuss/59825/java-solution-using-priorityqueue

Https://leetcode.com/discuss/67877/%08two-standard-dp-solutions

Https://leetcode.com/discuss/71549/o-n-java-easy-version-to-understand

Https://leetcode.com/discuss/53225/c-one-pass-simple-solution

Https://leetcode.com/discuss/52905/my-16ms-c-dp-solution-with-short-explanation

Https://leetcode.com/discuss/52710/java-solution-with-three-queues

Https://leetcode.com/discuss/52716/o-n-java-solution

Ugly number II

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.