Skip to content

feat: add solutions to lc problem: No.1201 #856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 98 additions & 3 deletions solution/1200-1299/1201.Ugly Number III/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,117 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:二分搜索**

根据题目提示结果在 [1, 2 * 10<sup>9</sup>] 的闭区间上,所以定义二分搜索的左边界 left=1,右边界 right=2e9。此时我们只需要在 [left,right] 的闭区间内找到一个最小的数 num,使其满足 [1,num] 内的丑数总数等于 n,则 num 就是第 n 个丑数。计算在 [1,num] 的范围内丑数的数目,即可以被 a、b 或 c 任意一个数整除的数的总数,其方法如下:

`f(num, a, b, c) = num/a + num/b + num/c - a⋂b - a⋂c - b⋂c + a⋂b⋂c`

- num/a 表示在 [1,num] 内可以整除 a 的数目,num/b 表示在 [1,num] 内可以整除 b 的数目,num/c 表示在 [1,num] 内可以整除 c 的数目。
- a⋂b 表示在 [1,num] 内可以同时整除 a 和 b 的数目,a⋂c 表示在 [1,num] 内可以同时整除 a 和 c 的数,b⋂c 表示在 [1,num] 内可以同时整除 b 和 c 的数。
- a⋂b⋂c 表示在 [1,num] 内可以同时整除 a、b 和 c 的数。
- a⋂b = num/least_common_multiple(a, b),其他情况依次类推。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def f(self, num: int, a: int, b: int, c: int) -> int:
return num // a + num // b + num // c - num // math.lcm(a, b) - num // math.lcm(a, c) - num // math.lcm(b, c) \
+ num // math.lcm(a, b, c)

def nthUglyNumber(self, n: int, a: int, b: int, c: int) -> int:
left, right = 1, int(2e9)
while left <= right:
mid = left + (right - left) // 2
if self.f(mid, a, b, c) < n:
left = mid + 1
else:
right = mid - 1
return left
```

### **Java**
### **Go**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
```go
func nthUglyNumber(n int, a int, b int, c int) int {
left, right := 1, int(2e9)
for left <= right {
mid := left + (right-left)/2
if f(mid, a, b, c) < n {
left = mid + 1
} else {
right = mid - 1
}
}
return left
}

func f(num int, a int, b int, c int) int {
return num/a + num/b + num/c - num/lcm(a, b) - num/lcm(a, c) - num/lcm(b, c) + num/lcm(lcm(a, b), c)
}

// Least common multiple
func lcm(a, b int) int {
// Greatest common divisor
gcd := func(x, y int) int {
for y != 0 {
if x < y {
x, y = y, x
}
x, y = y, x%y
}
return x
}
return a * b / gcd(a, b)
}
```

### **C++**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```cpp
class Solution {
public:
long gcd(long x, long y) {
while (y != 0) {
if (x < y)
swap(x, y);
long tmp = x % y;
x = y;
y = tmp;
}
return x;
}

long lcm(long x, long y) { return x * y / gcd(x, y); }

long f(int num, int a, int b, int c) {
long sumabc = long(num / a) + num / b + num / c;
long intersections = long(num / lcm(a, b)) + num / lcm(a, c) + num / lcm(b, c) - num / lcm(lcm(a, b), c);
return sumabc - intersections;
}

int nthUglyNumber(int n, int a, int b, int c) {
int left = 1, right = int(2e9);
while (left <= right) {
int mid = left + (right - left) / 2;
if (f(mid, a, b, c) < n) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;
}
};
```

### **...**
Expand Down
90 changes: 86 additions & 4 deletions solution/1200-1299/1201.Ugly Number III/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,95 @@
### **Python3**

```python

class Solution:
def f(self, num: int, a: int, b: int, c: int) -> int:
return num // a + num // b + num // c - num // math.lcm(a, b) - num // math.lcm(a, c) - num // math.lcm(b, c) \
+ num // math.lcm(a, b, c)

def nthUglyNumber(self, n: int, a: int, b: int, c: int) -> int:
left, right = 1, int(2e9)
while left <= right:
mid = left + (right - left) // 2
if self.f(mid, a, b, c) < n:
left = mid + 1
else:
right = mid - 1
return left
```

### **Java**

```java
### **Go**

```go
func nthUglyNumber(n int, a int, b int, c int) int {
left, right := 1, int(2e9)
for left <= right {
mid := left + (right-left)/2
if f(mid, a, b, c) < n {
left = mid + 1
} else {
right = mid - 1
}
}
return left
}

func f(num int, a int, b int, c int) int {
return num/a + num/b + num/c - num/lcm(a, b) - num/lcm(a, c) - num/lcm(b, c) + num/lcm(lcm(a, b), c)
}

// Least common multiple
func lcm(a, b int) int {
// Greatest common divisor
gcd := func(x, y int) int {
for y != 0 {
if x < y {
x, y = y, x
}
x, y = y, x%y
}
return x
}
return a * b / gcd(a, b)
}
```

### **C++**

```cpp
class Solution {
public:
long gcd(long x, long y) {
while (y != 0) {
if (x < y)
swap(x, y);
long tmp = x % y;
x = y;
y = tmp;
}
return x;
}

long lcm(long x, long y) { return x * y / gcd(x, y); }

long f(int num, int a, int b, int c) {
long sumabc = long(num / a) + num / b + num / c;
long intersections = long(num / lcm(a, b)) + num / lcm(a, c) + num / lcm(b, c) - num / lcm(lcm(a, b), c);
return sumabc - intersections;
}

int nthUglyNumber(int n, int a, int b, int c) {
int left = 1, right = int(2e9);
while (left <= right) {
int mid = left + (right - left) / 2;
if (f(mid, a, b, c) < n) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;
}
};
```

### **...**
Expand Down
34 changes: 34 additions & 0 deletions solution/1200-1299/1201.Ugly Number III/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:
long gcd(long x, long y) {
while (y != 0) {
if (x < y)
swap(x, y);
long tmp = x % y;
x = y;
y = tmp;
}
return x;
}

long lcm(long x, long y) { return x * y / gcd(x, y); }

long f(int num, int a, int b, int c) {
long sumabc = long(num / a) + num / b + num / c;
long intersections = long(num / lcm(a, b)) + num / lcm(a, c) + num / lcm(b, c) - num / lcm(lcm(a, b), c);
return sumabc - intersections;
}

int nthUglyNumber(int n, int a, int b, int c) {
int left = 1, right = int(2e9);
while (left <= right) {
int mid = left + (right - left) / 2;
if (f(mid, a, b, c) < n) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;
}
};
31 changes: 31 additions & 0 deletions solution/1200-1299/1201.Ugly Number III/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
func nthUglyNumber(n int, a int, b int, c int) int {
left, right := 1, int(2e9)
for left <= right {
mid := left + (right-left)/2
if f(mid, a, b, c) < n {
left = mid + 1
} else {
right = mid - 1
}
}
return left
}

func f(num int, a int, b int, c int) int {
return num/a + num/b + num/c - num/lcm(a, b) - num/lcm(a, c) - num/lcm(b, c) + num/lcm(lcm(a, b), c)
}

// Least common multiple
func lcm(a, b int) int {
// Greatest common divisor
gcd := func(x, y int) int {
for y != 0 {
if x < y {
x, y = y, x
}
x, y = y, x%y
}
return x
}
return a * b / gcd(a, b)
}
14 changes: 14 additions & 0 deletions solution/1200-1299/1201.Ugly Number III/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def f(self, num: int, a: int, b: int, c: int) -> int:
return num // a + num // b + num // c - num // math.lcm(a, b) - num // math.lcm(a, c) - num // math.lcm(b, c) \
+ num // math.lcm(a, b, c)

def nthUglyNumber(self, n: int, a: int, b: int, c: int) -> int:
left, right = 1, int(2e9)
while left <= right:
mid = left + (right - left) // 2
if self.f(mid, a, b, c) < n:
left = mid + 1
else:
right = mid - 1
return left