Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5eed621

Browse files
solves ugly number iii
1 parent 63ce905 commit 5eed621

File tree

3 files changed

+55
-24
lines changed

3 files changed

+55
-24
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
## Problems
1313

14+
<<<<<<< HEAD
1415
| # | Name | Solution | Youtube |
1516
| :--: | --------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------: |
1617
| 1 | [Two Sum](https://leetcode.com/problems/two-sum) | [![Java](assets/java.png)](src/TwoSum.java) [![Python](assets/python.png)](python/two_sum.py) | [![java-yt](assets/java-yt.png)](https://youtu.be/9wSL_7NN-A8) [![python-yt](assets/python-yt.png)](https://youtu.be/N5FXCTg0TDE) |

‎src/UglyNumberII.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ public class UglyNumberII {
66
public int nthUglyNumber(int n) {
77
final int[] dp = new int[n];
88
dp[0] = 1;
9-
int index2, index3, index5;
10-
index2 = index3 = index5 = 0;
119
int factor2 = 2, factor3 = 3, factor5 = 5;
10+
int index2 = 0, index3 = 0, index5 = 0;
1211

1312
for(int i = 1 ; i < n ; i++) {
1413
final int uglyNumber = min(factor2, factor3, factor5);
1514
dp[i] = uglyNumber;
16-
if(factor2 == uglyNumber) factor2 = 2 * dp[++index2];
17-
if(factor3 == uglyNumber) factor3 = 3 * dp[++index3];
18-
if(factor5 == uglyNumber) factor5 = 5 * dp[++index5];
15+
if (uglyNumber == factor2) factor2 = 2 * dp[++index2];
16+
if (uglyNumber == factor3) factor3 = 3 * dp[++index3];
17+
if (uglyNumber == factor5) factor5 = 5 * dp[++index5];
1918
}
2019

21-
return dp[n-1];
20+
return dp[dp.length - 1];
2221
}
2322

2423
private int min(int a, int b, int c) {

‎src/UglyNumberIII.java

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,59 @@
1-
import java.util.Arrays;
1+
// https://leetcode.com/problems/ugly-number-iii
2+
// T: O(log(MAX_VAL)) MAX_VAL = Integer.MAX_VAL here
3+
// S: O(1)
24

35
public class UglyNumberIII {
4-
public static int nthUglyNumber(int n, int a, int b, int c) {
5-
final int[] dp = new int[n + 1];
6-
dp[0] = 1;
7-
int factorA = a, factorB = b, factorC = c;
8-
int indexA = 0, indexB = 0, indexC = 0;
6+
private long a;
7+
private long b;
8+
private long c;
9+
private long lcm_a_b;
10+
private long lcm_a_c;
11+
private long lcm_b_c;
12+
private long lcm_a_b_c;
913

10-
for (int i = 1 ; i < dp.length ; i++) {
11-
final int uglyNumber = min(factorA, factorB, factorC);
12-
dp[i] = uglyNumber;
13-
if (uglyNumber == factorA) factorA = a * dp[++indexA];
14-
if (uglyNumber == factorB) factorB = b * dp[++indexB];
15-
if (uglyNumber == factorC) factorC = c * dp[++indexC];
14+
public int nthUglyNumber(int n, int a, int b, int c) {
15+
setValues(a, b, c);
16+
int left = 1, right = Integer.MAX_VALUE, middle;
17+
long factors;
18+
while (left <= right) {
19+
middle = left + (right - left) / 2;
20+
factors = numberOfFactors(middle);
21+
if (factors >= n) right = middle - 1;
22+
else left = middle + 1;
1623
}
17-
System.out.println(Arrays.toString(dp));
18-
return dp[dp.length - 1];
24+
return left;
1925
}
2026

21-
private static int min(int a, int b, int c) {
22-
return Math.min(a, Math.min(b, c));
27+
private void setValues(long a, long b, long c) {
28+
this.a = a;
29+
this.b = b;
30+
this.c = c;
31+
this.lcm_a_b = lcm(a, b);
32+
this.lcm_a_c = lcm(a, c);
33+
this.lcm_b_c = lcm(b, c);
34+
this.lcm_a_b_c = lcm(lcm_a_b, c);
2335
}
2436

25-
public static void main(String[] args) {
26-
System.out.println(nthUglyNumber(100, 2, 3, 5));
37+
/*
38+
* @param n number
39+
* @return will tell how many factors are there of a, b and c between 1 and n
40+
* e.g. if n=10 and a=2 b=3 so there are 7 factors {2, 3, 4, 6, 8, 9, 10}
41+
*/
42+
private long numberOfFactors(int n) {
43+
return n / a
44+
+ n / b
45+
+ n / c
46+
- n / lcm_a_b
47+
- n / lcm_a_c
48+
- n / lcm_b_c
49+
+ n / lcm_a_b_c;
50+
}
51+
52+
private long gcd(long a, long b) {
53+
return b == 0 ? a : gcd(b, a % b);
54+
}
55+
56+
private long lcm(long a, long b) {
57+
return (a * b) / gcd(a, b);
2758
}
2859
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /