forked from ecmadao/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNo264.ugly-number-II.js
More file actions
61 lines (56 loc) · 1.53 KB
/
No264.ugly-number-II.js
File metadata and controls
61 lines (56 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Difficulty:
* Medium
*
* Desc:
* Write a program to find the n-th ugly number.
* Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.
*
* Example:
* 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
*
* Note:
* 1 is typically treated as an ugly number, and n does not exceed 1690.
*
* Hints:
* 1. The naive approach is to call isUgly for every number until you reach the nth one.
* Most numbers are not ugly. Try to focus your effort on generating only the ugly ones.
* 2. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
* 3. The key is how 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 be Min(L1 * 2, L2 * 3, L3 * 5).
*/
var min = function(a, b, c) {
var tmp = a < b ? a : b;
return tmp < c ? tmp : c;
};
/**
* @param {number} n
* @return {number}
*/
var nthUglyNumber = function(n) {
var index2 = 0;
var index3 = 0;
var index5 = 0;
var index = 1;
var queue = [1];
while (queue.length < n) {
var num2 = queue[index2] * 2;
var num3 = queue[index3] * 3;
var num5 = queue[index5] * 5;
var num;
var num = min(num2, num3, num5);
if (num === num2) {
index2 += 1;
} else if (num === num3) {
index3 += 1;
} else {
index5 += 1;
}
if (num !== queue[index - 1]) {
queue[index] = num;
index += 1;
}
}
return queue[n - 1];
};