Skip to content

Commit

Permalink
contest 155
Browse files Browse the repository at this point in the history
  • Loading branch information
Azureki committed Sep 22, 2019
1 parent ec5bf84 commit f05e94d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 1200. Minimum Absolute Difference/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from collections import defaultdict


class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
d = defaultdict(list)
for i in range(len(arr) - 1):
d[arr[i + 1] - arr[i]].append((arr[i], arr[i + 1]))
return d[min(d.keys())]
34 changes: 34 additions & 0 deletions 1201. Ugly Number III/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution:
def nthUglyNumber(self, n, a, b, c):
gcd = lambda x, y: (gcd(y, x % y) if x % y else y)

# lcm = lambda x, y: x * y // gcd(x, y)
def lcm(x, y, *args):
if not args:
return x * y // gcd(x, y)
x = lcm(x, y)
y, *args = args
return lcm(x, y, *args)

a, b, c = sorted([a, b, c])
max_n = 2 * 10**9 + 1
left, right = 0, max_n
mid = 0

def get_ugly_num():
get_one = lambda x: mid // x
get_two = lambda x, y: mid // lcm(x, y)
num_a, num_b, num_c = get_one(a), get_one(b), get_one(c)
num_a_b, num_b_c, num_a_c = get_two(a, b), get_two(b, c), get_two(a, c)
num_a_b_c = mid // lcm(a, b, c)
return num_a + num_b + num_c - num_a_b - num_a_c - num_b_c + num_a_b_c

while left < right:
mid = (left + right) // 2
ugly_num = get_ugly_num()
if ugly_num < n:
left = mid + 1
else:
right = mid

return left

0 comments on commit f05e94d

Please sign in to comment.