forked from Akshaya-Amar/LeetCodeSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UglyNumberII.java
46 lines (35 loc) · 996 Bytes
/
UglyNumberII.java
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
/*
Source: https://leetcode.com/problems/ugly-number-ii/
Time: O(n), where n is a given positive integer
Space: O(n), an array is needed of length equal to the given positive integer n
*/
class Solution {
public int nthUglyNumber(int n) {
int[] uglyNumbers = new int[n];
uglyNumbers[0] = 1;
int multipleOf2 = 2;
int multipleOf3 = 3;
int multipleOf5 = 5;
int ptr2 = 0;
int ptr3 = 0;
int ptr5 = 0;
int index = 1;
while(index < n) {
int minValue = (multipleOf2 < multipleOf3) ? multipleOf2 : multipleOf3;
if(multipleOf5 < minValue) {
minValue = multipleOf5;
}
uglyNumbers[index++] = minValue;
if(minValue == multipleOf2) {
multipleOf2 = uglyNumbers[++ptr2] << 1;
}
if(minValue == multipleOf3) {
multipleOf3 = 3 * uglyNumbers[++ptr3];
}
if(minValue == multipleOf5) {
multipleOf5 = 5 * uglyNumbers[++ptr5];
}
}
return uglyNumbers[index - 1];
}
}