๐ 264. ไธๆฐ II
2022ๅนด10ๆ10ๆฅ
- algorithm
๐ 264. ไธๆฐ II
้พๅบฆ: ๐
้ฎ้ขๆ่ฟฐ
่งฃๆณ
class Solution {
public int nthUglyNumber(int n) {
// ๆ่ทฏ๏ผ
// 2a, 3b, 5c ไธญๅๆๅฐ
// a, b, c ๅๅซๆ็ๆฏไธๆ
int[] array = new int[n];
array[0] = 1;
int a = 0;
int b = 0;
int c = 0;
int res = 1;
for(int i = 1; i < n; i ++) {
res = Math.min(2 * array[a], Math.min(3 * array[b], 5 * array[c]));
array[i] = res;
if(res == 2 * array[a]) {
a ++;
}
if(res == 3 * array[b]) {
b ++;
}
if(res == 5 * array[c]) {
c ++;
}
}
return res;
}
}