Skip to content

Commit bc54bd4

Browse files
add 1201
1 parent 83d3887 commit bc54bd4

File tree

5 files changed

+130
-1
lines changed

5 files changed

+130
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -506,4 +506,4 @@ LeetCode
506506
|1198|[Find Smallest Common Element in All Rows](https://leetcode.com/contest/biweekly-contest-9/problems/find-smallest-common-element-in-all-rows/) | c | [c++](./src/1198-Find-Smallest-Common-Element-in-All-Rows/1198.cpp) |[python](./src/1198-Find-Smallest-Common-Element-in-All-Rows/1198.py)|[go](./src/1198-Find-Smallest-Common-Element-in-All-Rows/1198.go)|[js](./src/1198-Find-Smallest-Common-Element-in-All-Rows/1198.js)|Medium|
507507
|1199|[Minimum Time to Build Blocks](https://leetcode.com/contest/biweekly-contest-9/problems/minimum-time-to-build-blocks/) | c | [c++](./src/1199-Minimum-Time-to-Build-Blocks/1199.cpp) |[python](./src/1199-Minimum-Time-to-Build-Blocks/1199.py)|[go](./src/1199-Minimum-Time-to-Build-Blocks/1199.go)|[js](./src/1199-Minimum-Time-to-Build-Blocks/1199.js)|Hard|
508508
|1200|[Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/) | c | [c++](./src/1200-Minimum-Absolute-Difference/1200.cpp) |[python](./src/1200-Minimum-Absolute-Difference/1200.py)|[go](./src/1200-Minimum-Absolute-Difference/1200.go)|[js](./src/1200-Minimum-Absolute-Difference/1200.js)|Easy|
509-
|1201|[Ugly Number III](https://leetcode.com/problems/minimum-absolute-difference/) | c | [c++](./src/1200-Minimum-Absolute-Difference/1200.cpp) |[python](./src/1200-Minimum-Absolute-Difference/1200.py)|[go](./src/1200-Minimum-Absolute-Difference/1200.go)|[js](./src/1200-Minimum-Absolute-Difference/1200.js)|Easy|
509+
|1201|[Ugly Number III](https://leetcode.com/problems/ugly-number-iii/) | c | [c++](./src/1201-Ugly-Number-III/1201.cpp) |[python](./src/1201-Ugly-Number-III/1201.py)|[go](./src/1201-Ugly-Number-III/1201.go)|[js](./src/1201-Ugly-Number-III/1201.js)|Medium|

src/1201-Ugly-Number-III/1201.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution
2+
{
3+
public:
4+
int nthUglyNumber(int n, int a, int b, int c)
5+
{
6+
long long l = 1, r = 2000000000;
7+
while (l < r)
8+
{
9+
long long mid = (l + r) >> 1;
10+
if (cnt(mid, a, b, c) < n) l = mid + 1;
11+
else r = mid;
12+
}
13+
return l;
14+
}
15+
16+
long long lcm2(int x, int y)
17+
{
18+
return x*1ll*y/__gcd(x, y);
19+
}
20+
21+
long long lcm3(int x, int y, int z)
22+
{
23+
long long res = x*1ll*y/__gcd(x, y);
24+
return res*z/__gcd(res, z*1ll);
25+
}
26+
27+
int cnt(long long k, int x, int y, int z)
28+
{
29+
return k/x + k/y + k/z - k/lcm2(x, y) -k/lcm2(x, z) - k/lcm2(y, z) + k/lcm3(x, y, z);
30+
}
31+
};

src/1201-Ugly-Number-III/1201.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
func nthUglyNumber(n int, a int, b int, c int) int {
2+
var l, r int64
3+
l, r = 1, 2000000000
4+
for l < r {
5+
mid := (l + r) >> 1
6+
if cnt(mid, int64(a), int64(b), int64(c)) < int64(n) {
7+
l = mid + 1
8+
} else {
9+
r = mid
10+
}
11+
}
12+
return int(l)
13+
}
14+
15+
func gcd(x, y int64) int64 {
16+
for y > 0 {
17+
x, y = y, x % y
18+
}
19+
return x
20+
}
21+
22+
func lcm2(x, y int64) int64 {
23+
return x * y / gcd(x, y)
24+
}
25+
26+
func lcm3(x, y, z int64) int64 {
27+
res := x * y / gcd(x, y)
28+
return res * z / gcd(res, z)
29+
}
30+
31+
func cnt(k, x, y, z int64) int64 {
32+
return k/x + k/y + k/z - k/lcm2(x, y) -k/lcm2(x, z) - k/lcm2(y, z) + k/lcm3(x, y, z)
33+
}

src/1201-Ugly-Number-III/1201.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} a
4+
* @param {number} b
5+
* @param {number} c
6+
* @return {number}
7+
*/
8+
var nthUglyNumber = function(n, a, b, c) {
9+
function gcd(x, y) {
10+
while (y) {
11+
var t = x;
12+
x = y;
13+
y = t % y;
14+
}
15+
return x;
16+
}
17+
18+
function lcm2(x, y) {
19+
return Math.floor(x * y / gcd(x, y));
20+
}
21+
22+
function lcm3(x, y, z) {
23+
var res = Math.floor(x * y / gcd(x, y));
24+
return Math.floor(res * z / gcd(res, z));
25+
}
26+
27+
function cnt(k, x, y, z) {
28+
return Math.floor(k/x) + Math.floor(k/y) + Math.floor(k/z) -
29+
Math.floor(k/lcm2(x, y)) - Math.floor(k/lcm2(x, z)) -
30+
Math.floor(k/lcm2(y, z)) + Math.floor(k/lcm3(x, y, z));
31+
}
32+
33+
var l = 1, r = 2000000000;
34+
while (l < r) {
35+
var mid = Math.floor((l + r)/2);
36+
if (cnt(mid, a, b, c) < n) l = mid + 1;
37+
else r = mid;
38+
}
39+
return l;
40+
};

src/1201-Ugly-Number-III/1201.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def nthUglyNumber(self, n: int, a: int, b: int, c: int) -> int:
3+
def gcd(x, y):
4+
while y:
5+
x, y = y, x % y
6+
return x
7+
8+
def lcm2(x, y):
9+
return x*y//gcd(x, y)
10+
11+
def lcm3(x, y, z):
12+
res = x * y // gcd(x, y)
13+
return res * z // gcd(z, res)
14+
15+
def cnt(k, x, y, z):
16+
return k//x + k//y + k//z - k//lcm2(x,y) -k//lcm2(x, z) - k//lcm2(y, z) + k//lcm3(x, y, z)
17+
18+
l, r = 1, 2*10**9
19+
while l < r:
20+
mid = (l + r) >> 1
21+
if cnt(mid, a, b, c) < n:
22+
l = mid + 1
23+
else:
24+
r = mid
25+
return l

0 commit comments

Comments
 (0)