-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathugly-number-ii.js
82 lines (75 loc) · 1.96 KB
/
ugly-number-ii.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* @param {number} n
* @return {number}
*/
var nthUglyNumber = function(n) {
let p2 = p3 = p5 = 0
const ans = [1]
while (ans.length < n) {
const min = Math.min(2 * ans[p2], 3 * ans[p3], 5 * ans[p5])
ans.push(min)
if(min === 2 * ans[p2]) p2++
if(min === 3 * ans[p3]) p3++
if(min === 5 * ans[p5]) p5++
}
return ans[ans.length - 1]
};
/**
* @param {number} n
* @return {number}
*/
var nthUglyNumber = function(n) {
const minHeap = new Heap()
minHeap.push(1)
let ans
while(n--) {
ans = minHeap.pop()
if (ans % 5 === 0) {
minHeap.push(ans * 5)
} else if (ans % 3 === 0) {
minHeap.push(ans * 3)
minHeap.push(ans * 5)
} else {
minHeap.push(ans * 2)
minHeap.push(ans * 3)
minHeap.push(ans * 5)
}
}
return ans
};
class Heap {
constructor() {
this.heap = []
}
push(val) {
this.heap.push(val)
this._sortBack()
}
pop() {
const val = this.heap[0]
const back = this.heap.pop()
if(this.heap.length) {
this.heap[0] = back
this._sortFront()
}
return val
}
_sortBack() {
let i = this.heap.length - 1
while(i > 0 && this.heap[i] < this.heap[Math.floor((i - 1) / 2)]) {
[this.heap[i], this.heap[Math.floor((i - 1) / 2)]] = [this.heap[Math.floor((i - 1) / 2)], this.heap[i]]
i = Math.floor((i - 1) / 2)
}
}
_sortFront() {
let i = 0
while (i * 2 + 1 < this.heap.length) {
let temp = i
if (this.heap[temp] > this.heap[i * 2 + 1]) temp = i * 2 + 1
if (i * 2 + 2 < this.heap.length && this.heap[temp] > this.heap[i * 2 + 2]) temp = i * 2 + 2
if (temp === i) break
[this.heap[temp], this.heap[i]] = [this.heap[i], this.heap[temp]]
i = temp
}
}
}