inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
if m == 2:
return position[-1] - position[0]
def checkPossible(ans0):
preV = position[0]
leftM = m - 1
for num in position[1:]:
if num - preV >= ans0:
leftM -= 1
preV = num
if leftM == 0:
return True
return False
l = 0
r = position[-1]
ans = position[1]-position[0]
while r > l:
mid = (r+l)//2
# print(l, r, mid)
if checkPossible(mid):
ans = mid
l = mid + 1
else:
r = mid
return ans
# dp = {}
# minDelta = [position[i]-position[i-1] for i in range(1, n)]
# # print(position)
# # print(minDelta)
# for i in range(n-3, -1, -1):
# if minDelta[i] > minDelta[i+1]:
# minDelta[i] = minDelta[i+1]
# def placeABall(preI, m0):
# if (preI, m0) in dp:
# return dp[(preI, m0)]
# if m0 == 1:
# return position[-1] - position[preI]
# if n-preI-1 == m0:
# subAns = minDelta[preI]
# dp[(preI, m0)] = subAns
# return subAns
# subAns = 0
# l = preI+1
# r = n-m0
# if position[l] - position[preI] >= placeABall(l, m0-1):
# subAns = placeABall(l, m0-1)
# elif position[r] - position[preI] <= placeABall(r, m0-1):
# subAns = position[r] - position[preI]
# else:
# while l < r:
# m = (l+r)//2
# temp = placeABall(m, m0-1)
# if position[m] - position[preI] < temp:
# l = m + 1
# elif position[m] - position[preI] > temp:
# r = m
# else:
# l = m
# break
# subAns = (min(position[l] - position[preI], temp))
# if l + 1 <= n-m0:
# subAns = max(subAns, (min(position[l+1] - position[preI], placeABall(l+1, m0-1))))
# if l - 1 >= preI + 1:
# subAns = max(subAns, (min(position[l-1] - position[preI], placeABall(l-1, m0-1))))
# # for i1 in range(preI+1, n-m0+1):
# # subAns = max(subAns, min(position[i1] - position[preI], placeABall(i1, m0-1)))
# dp[(preI, m0)] = subAns
# return subAns
# return placeABall(0, m-1)
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
hi = position[-1] - position[0] + 1
lo = 0
def x(target):
currMin = position[0]
count = 1
for pos in position:
if pos - currMin >= target:
currMin = pos
count += 1
if count >= m:
return False
return True
while lo < hi:
mid = lo + (hi - lo) // 2
if x(mid):
hi = mid
else:
lo = mid + 1
return lo - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def distributable(n):
pos = len(position) - 1
balls = m
while pos >= 0 and balls:
balls -= 1
np = position[pos] - n
while pos >= 0 and position[pos] > np:
pos -= 1
return not balls
position = sorted(position)
lo, hi = 1, (position[-1] - position[0]) // (m -1) + 1
ans = lo
while lo < hi:
mid = lo + (hi - lo) // 2
if distributable(mid):
ans = mid
lo = mid + 1
else:
hi = mid
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position = sorted(position)
l = 1
r = position[-1] - position[0]
def check(diff, m):
ctr = 1
prev = 0
for i in range(1, len(position)):
if ctr==m:
return True
if position[i]-position[prev]>=diff:
ctr+=1
prev = i
if ctr==m:
return True
return False
val = -10**10
while l<=r:
mid = (l+r)//2
if check(mid, m):
val = max(val, mid)
l = mid+1
else:
r = mid-1
return val
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n=len(position)
position.sort()
def count(d):
cur=position[0]
ans=1
for i in range(1,n):
if position[i]-cur>=d:
ans+=1
cur=position[i]
return ans
l,r=0,position[-1]-position[0]
while l<r:
mid=r-(r-l)//2
if count(mid)>=m:
l=mid
else:
r=mid-1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def distributable(n):
pos = len(position) - 1
balls = m
while pos >= 0 and balls:
balls -= 1
np = position[pos] - n
while pos >= 0 and position[pos] > np:
pos -= 1
return not balls
position = sorted(position)
lo, hi = 1, (position[-1] - position[0]) // (m -1) + 1
ans = lo
while lo < hi:
mid = lo + (hi - lo) // 2
if distributable(mid):
ans = mid
lo = mid + 1
else:
hi = mid
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def isOK(x):
rest = m - 1
pre = position[0]
for p in position[1:]:
if p - pre >= x:
pre = p
rest -= 1
if rest == 0:
return True
return False
left = 1
right = position[-1] - position[0]
while left < right:
mid = (left + right + 1) // 2
# print(f'{mid=}')
# print(f'{isOK(mid)=}')
if isOK(mid):
left = mid
else:
right = mid - 1
return right
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
from array import array
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position = sorted(position)
position = array('i', position)
high = position[-1] - position[0]
low = high
for i in range(1, len(position)):
low = min(position[i] - position[i - 1], low)
def count(step):
c = 1
pivot = 0
for i in range(1, len(position)):
if step <= (position[i] - position[pivot]):
c += 1
pivot = i
return c
res = None
while low <= high:
mid = low + (high - low) // 2
num = count(mid)
if num >= m:
res = mid
low = mid+1
if num < m:
high = mid-1
return res
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def check(arr, n, m, mid):
magnet=1
pos=arr[0]
for i in range(1, n):
if arr[i]-pos>=mid:
magnet+=1
pos=arr[i]
if magnet==m:
return 1
return 0
position.sort()
l=0
n=len(position)
r=position[n - 1]
res=0
while l<=r:
mid=(l+r)//2
if not check(position, n, m, mid):
r=mid-1
else:
res=max(res, mid)
l=mid + 1
return res
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
def count(d):
ans = 1
pos = position[0]
for i in range(1,n):
if position[i]-pos>=d:
ans+=1
pos = position[i]
return ans
l = 0
r = position[-1] - position[0]
while l<r:
mid = (l+r+1)//2
if count(mid)>=m:
l = mid
else:
r = mid-1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
lo,hi=1,position[-1]-position[0]
while lo<hi:
mi,t,y=(lo+hi+1)//2,1,position[0]
for x in position:
if x-y>=mi: y,t=x,t+1
if t<m: hi=mi-1
else: lo=mi
return lo
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position, m: int) -> int:
# binary search and check easy
# this function will check if all the balls can be placed at a distance of m or not. Then we just binary search all the
# distances. O(nlogn)
position.sort()
def check(mid):
curr = position[0]
placed = 1
for i in range(len(position)):
if position[i] >= curr + mid:
placed += 1
curr = position[i]
return placed >= m
l, r = 1, 10 ** 9 + 1
while l < r:
mid = (l + r + 1) // 2
if check(mid):
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, a: List[int], m: int) -> int:
def check(d):
x, k = 0, m
for i in range(len(a)):
if a[i] >= x:
x, k = a[i] + d, k - 1
if k == 0: break
return k == 0
a.sort()
l, r = 1, (max(a) - min(a)) // (m-1)
while l < r:
d = r - (r - l) // 2
if check(d): l = d
else: r = d - 1
return r if check(r) else l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n=len(position)
def posb(t):
k=0
s=0
for i in range(n-1):
k+=position[i+1]-position[i]
if k>=t:
k=0
s+=1
return s>=m-1
hgh=position[-1]-position[0]
low=0
while low<hgh:
mid=hgh-(hgh-low)//2
if posb(mid):
low=mid
else:
hgh=mid-1
return low
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
# sort balls
position.sort()
# max number of balls that can preserve a min difference of diff between any two balls
def count_balls(diff):
nballs, cur = 1, position[0]
for i in range(1, len(position)):
if position[i] - cur >= diff:
nballs += 1
cur = position[i]
return nballs
# m can range from 1 to position[-1] - position[0]
left, right = 1, position[-1] - position[0]
while left <= right:
mid = (left + right) // 2
if count_balls(mid) >= m:
left = mid + 1
else:
right = mid - 1
return right
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
def count(position,m,n,d):
ans,curr=1,position[0]
for i in range(1,n):
if position[i]-curr>=d:
ans+=1
curr = position[i]
if ans==m:
return True
return False
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
l,r = 1,10**9+7
res = -1
while l<r:
mid = (l+r)//2
if count(position,m,n,mid):
res = max(res,mid)
l = mid+1
else:
r = mid
return res
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
position.sort()
# Counts number of balls that can be placed into baskets if minimum distance is d
# Find maximum d where count(d) == m
def count(d):
ans, curr = 1, position[0]
for i in range(1, n):
if position[i] - curr >= d:
ans += 1
curr = position[i]
return ans
l, r = 0, position[-1] - position[0]
while l < r:
mid = r - (r - l) // 2
if count(mid) >= m:
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, A: List[int], m: int) -> int:
A.sort()
n = len(A)
l, r = 1, A[-1] - A[0]
def get_m(d):
res = 1
i = 1
prev = A[0]
while i < n:
if A[i] - prev >= d:
res += 1
prev = A[i]
i += 1
return res
while l < r:
mid = r - (r - l) // 2
if get_m(mid) < m:
r = mid - 1
else:
l = mid
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
length = len(position)
position.sort()
def count(d):
minmax = 1
curr = position[0]
for i in range(1, length):
if position[i] - curr >= d:
minmax += 1
curr = position[i]
return minmax
lo, hi = 0, position[-1] - position[0]
while lo < hi:
mid = hi - (hi - lo) // 2
if count(mid) >= m:
lo = mid
else:
hi = mid - 1
return lo
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def ispossible(A,force,n):
n-=1
last=A[0]
for i in range(1,len(A)):
if n>0:
if A[i]-last>=force:
n-=1
last=A[i]
else:
break
if n==0:
return True
return False
A=position
A.sort()
l=1
r=A[-1]
ans=1
while r>=l:
mid=(r+l)//2
if ispossible(A,mid,m):
ans=mid
l=mid+1
else:
r=mid-1
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
l, r = 1, position[-1] - position[0] + 1
def isInvalid(val):
ball = 1
previous = position[0]
for p in position:
if p - previous < val:
continue
ball += 1
previous = p
return ball < m
while l < r:
mid = l + (r - l) // 2
if isInvalid(mid):
r = mid
else:
l = mid + 1
return l - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, a: List[int], m: int) -> int:
def check(d):
x, k = 0, m
for i in range(len(a)):
if a[i] >= x:
x, k = a[i] + d, k - 1
if k == 0: break
return k == 0
a.sort()
l, r = 1, (max(a) - min(a)) // (m-1)
while l+1 < r:
d = (l + r) // 2
if check(d): l = d
else: r = d - 1
return r if check(r) else l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
high=position[-1]-position[0]
low=1
output=[0]
k=[m]
def check(distance):
previous=-1
m=k[-1]
previous=position[0]
m-=1
if m==0:
return True
for i in range(1,len(position)):
if position[i]-previous>=distance:
previous=position[i]
m-=1
if m==0:
break
if m==0:
return True
return False
def binary(low,high):
mid=(low+high)//2
if low>high:
return
if check(mid):
output[0]=mid
binary(mid+1,high)
else:
binary(low,mid-1)
binary(low,high)
return output[-1]
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
# class Solution:
# def maxDistance(self, position: List[int], m: int) -> int:
# #首先排序
# position.sort()
# print(position)
# left = 1000000000
# right = position[-1] - position[0]
# for i in range(len(position)-1):
# diff = abs(position[i] - position[i+1])
# left = min(left, diff)
# def check(diff, p, m):
# m -= 1
# last = p[0]
# for i in range(1, len(p)):
# if abs(p[i] - last) >= diff:
# m -= 1
# last = p[i]
# if m <= 0:
# print(diff, \"True\")
# return True
# else:
# pass
# print(diff, \"False\")
# return False
# print(\"left\", left, \"right\", right)
# while left < right:
# mid = (left + right) // 2
# if check(mid, position, m) == True:
# left = mid
# if left == right:
# print(\"find 1\", left)
# break
# if left + 1 == right:
# if check(right, position, m):
# left = right
# print(\"find 2\", left)
# break
# else:
# right = mid - 1
# print(\"find 3\", left)
# return left
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
#首先排序
position.sort()
# print(position)
distance = [0 for _ in range(len(position)-1)]
for i in range(len(position)-1):
diff = position[i+1] - position[i]
distance[i] = diff
left = min(distance)
right = position[-1] - position[0]
def check(diff, m):
m -= 1
pre_dis = 0
for i in range(0, len(distance)):
if distance[i]+pre_dis >= diff:
m -= 1
if m <= 0:
# print(diff, \"True\")
return True
pre_dis = 0
else:
pre_dis += distance[i]
# print(diff, \"False\")
return False
# print(\"left\", left, \"right\", right)
while left < right:
mid = (left + right+1) // 2
if check(mid, m) == True:
left = mid
# if left == right:
# print(\"find 1\", left)
# break
# if left + 1 == right:
# if check(right, position, m):
# left = right
# print(\"find 2\", left)
# break
else:
right = mid - 1
# print(\"find 3\", left)
return left
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
if m == 2:
return position[-1]-position[0]
print(position)
left, right = 1, position[-1]-position[0]
def valid(mid):
count = 1
last = position[0]
for i in range(1, len(position)):
if position[i] - last>=mid:
#print('-',position[i], last)
last = position[i]
count += 1
#print(mid, count)
return count>=m
while left<right:
mid = (right-left+1)//2+left
#print(mid)
if valid(mid):
left = mid
else:
right = mid - 1
return left
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, pos: List[int], m: int) -> int:
pos.sort()
def check(force, m):
last = pos[0]
m -= 1
for i in range(1, len(pos)):
if pos[i] - last >= force:
last = pos[i]
m -= 1
if not m: return True
return False
left, right = 2, 10**9
while left <= right:
mid = left + (right - left) // 2
if check(mid, m):
left = mid+1
else:
right = mid-1
return right
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position = sorted(position)
lo = 1
hi = position[-1]
while (lo < hi):
mid = lo + (hi - lo) // 2 + 1
if (self.can_be_put(position, m , mid)):
lo = mid
else:
hi = mid - 1
return lo
def can_be_put(self, position, m, force):
next_pos = position[0]
for i in range(len(position)):
if (next_pos <= position[i]):
m -= 1
next_pos = position[i] + force
return m <= 0
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
def check(force):
k = m - 1
pre = 0
i = 1
while i < n:
while i < n and position[i] - position[pre] < force:
i += 1
if i < n:
k -= 1
if k == 0:
return True
pre = i
i += 1
return False
lo, hi = 1, position[-1]
while lo < hi:
mi = lo + (hi - lo) // 2
if check(mi):
lo = mi + 1
else:
hi = mi
return lo - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
# count number of balls that can be put in basket with min_force d
def count(d):
ans, curr = 1, position[0]
for i in range(1, n):
if position[i] - curr >= d:
ans += 1
curr = position[i]
return ans
l, r = 1, position[-1] - position[0]
while l < r:
f = r - (r - l) // 2 # l + (r - l) // 2
if count(f) >= m:
l = f
else:
r = f - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
N = len(position)
def place(d):
pre = position[0]
idx = 1
for i in range(m-1):
while idx<N:
if position[idx]>=pre+d:
break
else:
idx+=1
if idx==N:
return False
else:
pre = position[idx]
return True
l = 1
r = position[-1]-position[0]
while l<r:
mid = (l+r+1)//2
if place(mid):
l = mid
else:
r = mid-1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
l = 1
r = (position[-1]-position[0])//(m-1)
#print(l,r)
while (l<=r):
mid = (l+r)//2
placed = 1
distance = 0
start = position[0]
for i in range(1,len(position)):
distance = position[i]-start
if distance >= mid:
placed +=1
distance = 0
start = position[i]
#print(placed)
if placed >=m:
answer = mid #we placed all balls
l = mid +1
else:
r = mid -1
return(answer)
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
high=position[-1]-position[0]
low=1
output=[0]
k=[m]
def check(distance):
previous=-1
m=k[-1]
for i in range(len(position)):
if m==0:
break
if i==0:
previous=position[0]
m-=1
continue
if position[i]-previous>=distance:
previous=position[i]
m-=1
if m==0:
return True
return False
def binary(low,high):
mid=(low+high)//2
if low>high:
return
if check(mid):
output[0]=mid
binary(mid+1,high)
else:
binary(low,mid-1)
binary(low,high)
return output[-1]
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def isPossible(dist):
count = 1
prev = position[0]
for x in position[1:]:
if x - prev >= dist:
count += 1
prev = x
if count == m:
return True
return False
left, right = 0, position[-1]-position[0]
while left <= right:
mid = left + (right-left)//2
if isPossible(mid):
left = mid + 1
else:
right = mid - 1
return right
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
position.sort()
def count(d):
ans, curr = 1, position[0]
for i in range(1, n):
if position[i] - curr >= d:
ans += 1
curr = position[i]
return ans
l, r = 0, position[-1] - position[0]
while l < r:
mid = r - (r - l) // 2
if count(mid) >= m:
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def possible(self, position, dist, m):
prev = float('-inf')
cnt = 0
for x in position:
if x < prev + dist:
continue
cnt += 1
prev = x
return cnt >= m
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
low, high = 0, max(position)
while low + 1 < high:
mid = (low + high) // 2
if self.possible(position, mid, m):
low = mid
else:
high = mid
if self.possible(position, high, m):
return high
return low
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def check(f):
nonlocal position
nonlocal m
nonlocal res
for i in range(len(position)):
balls_cnt = 1
last_ball_pos = 0
for i in range(1, len(position)):
last_ball_val_pos = position[last_ball_pos]
curr_ball_val_pos = position[i]
cur_force = curr_ball_val_pos - last_ball_val_pos
if cur_force >= f:
balls_cnt +=1
last_ball_pos = i
if balls_cnt == m:
res = max(res, f)
return True
return False
res = 0
position.sort()
left = 1
right = position[-1]
while left < right:
mid = left + (right - left) // 2
if check(mid):
# add to answer, check if can do better increase mid and look up in right part
left = mid + 1
else:
right = mid
return res
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
N = len(position)
left, right = 0, position[-1] - position[0]
while left + 1 < right:
mid = int((right - left) / 2 + left)
if self.count(mid, position) >= m:
left = mid
else:
right = mid
if self.count(right, position) == m:
return right
return left
def count(self, n: int, position: List[int]) -> int:
ans, curr = 1, position[0]
for i in range(1, len(position)):
if position[i] - curr >= n:
curr = position[i]
ans += 1
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def good(d):
c, prev = 1, 0
for i in range(1,len(position)):
if position[i] - position[prev] >= d:
c += 1
prev = i
if c == 0:
break
return c
position.sort()
l, r = 0, position[-1] - position[0]
while l <= r:
mid = (r + l) // 2
if good(mid) >= m:
l = mid + 1
else:
r = mid - 1
return r
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self,position, m) -> int:
position.sort()
l = 0
r = position[-1]-position[0]
def isValid(dist):
used = 1
curr = position[0]
for j in range(1,len(position)):
if position[j]-curr>=dist:
used+=1
curr = position[j]
return used
while l<r:
# 1 2 3 4 5
# l r
# l-> bigger
# <-r smaller
d = r-(r-l)//2
used = isValid(d)
if used>=m:
l = d
else:
r = d-1
return r
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
position = sorted(position)
def count(d, position):
m_num, cur = 1, position[0]
for p in position:
if p - cur >= d:
m_num += 1
cur = p
return m_num
l = 0
r = position[-1] - position[0]
while l < r:
mid = l + (r-l)//2
m_num = count(mid+1, position)
if m_num < m:
r = mid
else:
l = mid + 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
# n = len(position)
# position.sort()
# res = 0
# def feasible(distance):
# balls, curr = 1, position[0]
# for i in range(1, n):
# if position[i]-curr >= distance+1:### +1 to account for buckets
# balls += 1
# curr = position[i]
# return balls >= m
# left, right = 0, position[-1]-position[0]
# while left<right:
# mid = left + (right-left)//2
# if feasible(mid):
# left = mid+1
# else:
# right = mid
# return left
# # return right
############################################
n = len(position)
position.sort()
def count(d):
ans, curr = 1, position[0]
for i in range(1, n):
if position[i] - curr >= d:
ans += 1
curr = position[i]
return ans >= m
l, r = 0, position[-1] - position[0]
while l < r:
mid = r - (r - l) // 2 ###speed_up
# mid = l + (r - l) // 2 ###TLE
print((l, r))
if count(mid):
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
# we will binary search for the result
position.sort()
# the minimum possible distance between balls is 1
lo = 1
# the maximum possible distance is the distance from the end to the start
hi = position[-1] - position[0]
def isPossible(minDistance):
remaining = m
prev = float('-inf')
for p in position:
if p - prev >= minDistance:
prev = p
remaining -= 1
return remaining < 1
while lo < hi:
mid = hi - ((hi - lo) >> 1)
if isPossible(mid):
lo = mid
else:
hi = mid - 1
return lo
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
position.sort()
l,r = 1, position[-1]-position[0]
res = r
while l <= r:
mid = (l+r)//2
prev,i,cnt = position[0],1,m-1
while i < n and cnt > 0:
if position[i] - prev >= mid:
cnt -= 1
prev = position[i]
i += 1
if cnt > 0:
r = mid - 1
else:
res = mid
l = mid + 1
return res
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
position.sort()
def count(d):
num, curr = 1, position[0]
for i in range(1, n):
if position[i] - curr >= d:
num += 1
curr = position[i]
return num
l, r = 0, position[-1] - position[0]
while l < r:
mid = r - (r - l) // 2
if count(mid) >= m:
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, A: List[int], m: int) -> int:
A.sort()
def valid(i):
cnt = 1
cur = A[0]
for x in A[1:]:
if x - cur >= i:
cnt += 1
cur = x
if cnt >= m:
return True
return False
#print(valid(1), valid(2), valid(3), valid(4))
l, r = 1, A[-1]
while l < r:
mid = (l + r) // 2
if valid(mid):
l = mid + 1
else:
r = mid
return l - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def isPossible(mid, m):
m -= 1
i = 1
prev = position[0]
while i < n and m:
if position[i] - prev >= mid:
prev = position[i]
m -= 1
i += 1
return m == 0
position.sort()
n = len(position)
lo = hi = position[-1] - position[0]
for i in range(n - 1):
lo = min(lo, position[i + 1] - position[i])
ans = 0
while lo <= hi:
mid = lo + (hi - lo)//2
if isPossible(mid, m):
ans = mid
lo = mid + 1
else:
hi = mid - 1
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
left,right = 0,position[-1]-position[0] # search space is sorted
def count_balls(force):
balls,prev = 1,position[0]
for cur in position[1:]:
if cur-prev >= force:
balls += 1
prev = cur
return balls
while left < right:
# mid = left + (right-left)//2
mid = right - (right-left) // 2
# mid = (left+right+1)//2
if count_balls(mid) >= m:
left = mid
else:
right = mid - 1
return left
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def checkForce(force, positions, m):
i, prev = 1, positions[0]
while m > 0 and i < len(positions):
while positions[i] - force < prev:
i += 1
if i >= len(positions): return False
prev = positions[i]
m -= 1
i += 1
return m <= 0
l, r = 0, position[-1]
while l < r:
mid = r - (r - l) // 2
# force is acceptable, let's see if we can achieve a higher force
if checkForce(mid, position, m-1):
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def count(d):
res = 1
cur = position[0]
for i in range(1, n):
if position[i] - cur >= d:
res += 1
cur = position[i]
return res
n = len(position)
position.sort()
l, r = 0, position[-1] - position[0]
while l < r:
mid = r - (r - l) // 2
if count(mid) >= m:
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
w = (position[-1] - position[0]) // (m-1)
print(position)
def count(w, m):
idx = 0
m -= 1
for i in range(1, len(position)):
if position[i] - position[idx] >= w:
idx = i
m -= 1
if m == 0:
break
return m == 0
if count(w, m):
return w
else:
l ,r = 0, w
while l < r:
mid = r - (r - l) // 2
if count(mid, m):
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def count(dist):
last_pos = position[0]
cnt = 1
for pos in position:
if pos - last_pos >= dist:
cnt += 1
last_pos = pos
return cnt
# Binary Search on the answer
l = 1
r = position[-1] - position[0]
while l < r:
mid = (l + r + 1) // 2
if count(mid) < m:
r = mid-1
else:
l = mid
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def findcount(d):
res = 1
current = position[0]
i = 1
while i < len(position):
if position[i] >= current+d:
current = position[i]
i+=1
res+=1
else :
i+=1
return res
l, r = 0, position[-1] - position[0]
while l < r:
mid = r - (r - l) // 2
if findcount(mid) >= m:
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n=len(position)
res=-1
left=position[0]
right=position[n-1]
while left<right:
mid=(left+right)//2
if self.isFeasible(position,mid,m):
res=max(res,mid)
left=mid+1
else:
right=mid
if res==-1:
left=0
right=position[0]
while left<right:
mid=(left+right)//2
if self.isFeasible(position,mid,m):
res=max(res,mid)
left=mid+1
else:
right=mid
return res
def isFeasible(self,arr,mid,m):
n=len(arr)
pos=arr[0]
count=1
for i in range(1,n):
if arr[i]-pos>=mid:
pos=arr[i]
count+=1
if count==m:
return True
return False
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position, m):
position.sort()
N = len(position)
l = math.inf
for i in range(1, N):
l = min(l, position[i] - position[i-1])
r = position[-1] - position[0] + 1
def check(k):
last = position[0]
res = 0
for i in range(1, N):
if position[i] - last >= k:
res += 1
last = position[i]
return res
while l < r:
k = (l + r) // 2
if check(k) < m - 1:
r = k
else:
l = k + 1
return r - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n=len(position)
def distance(d):
p=0 # current position
c=1 # count of balls put into basket
while p<n-1:
for i in range(p,n):
if position[i]-position[p]>=d:
c+=1
break
p=i
if c==m:
return True
return False
l,r=0,position[-1]
while l<r:
md=r-(r-l)//2
#print(md,distance(md))
if distance(md):
l=md
else:
r=md-1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def place(d):
print(d)
ans = 1
pos = position[0]+d
j = 1
while pos <= position[-1]:
while j < len(position) and position[j] < pos:
j += 1
if j == len(position):
break
ans += 1
pos = position[j]+d
j += 1
print('ans')
print(ans)
return ans
l = 0
r = position[-1]-position[0]
while l <= r:
mid = (l+r)//2
if place(mid)>=m:
l = mid+1
else:
r = mid-1
return l-1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self,position, m) -> int:
position.sort()
l = 0
r = position[-1]-position[0]
def isValid(dist):
used = 1
curr = position[0]
for j in range(1,len(position)):
if position[j]-curr>=dist:
used+=1
curr = position[j]
return used
while l<r:
d = r-(r-l)//2
used = isValid(d)
if used>=m:
l = d
else:
r = d-1
return r
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
lo=0
hi=position[-1]-position[0]
n=len(position)
def count(d):
ans, curr = 1, position[0]
for i in range(1, n):
if position[i] - curr >= d:
ans += 1
curr = position[i]
return ans
while lo<hi:
#mid= (lo+hi+1)//2
mid=hi-(hi-lo)//2
res=count(mid)
if res>=m:
lo=mid
elif res<m:
hi=mid-1
return lo
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def check(d):
i = 0
j = 1
for _ in range(m-1):
while j < len(position) and position[j] - position[i] < d:
j += 1
if j >= len(position):
return False
i = j
j = j + 1
return True
a = 1
b = position[-1] - position[0]
ans = 0
while a <= b:
c = (a + b) // 2
if check(c):
ans = c
a = c + 1
else:
b = c - 1
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
# class Solution:
# def maxDistance(self, position: List[int], m: int) -> int:
# #首先排序
# position.sort()
# print(position)
# left = 1000000000
# right = position[-1] - position[0]
# for i in range(len(position)-1):
# diff = abs(position[i] - position[i+1])
# left = min(left, diff)
# def check(diff, p, m):
# m -= 1
# last = p[0]
# for i in range(1, len(p)):
# if abs(p[i] - last) >= diff:
# m -= 1
# last = p[i]
# if m <= 0:
# print(diff, \"True\")
# return True
# else:
# pass
# print(diff, \"False\")
# return False
# print(\"left\", left, \"right\", right)
# while left < right:
# mid = (left + right) // 2
# if check(mid, position, m) == True:
# left = mid
# if left == right:
# print(\"find 1\", left)
# break
# if left + 1 == right:
# if check(right, position, m):
# left = right
# print(\"find 2\", left)
# break
# else:
# right = mid - 1
# print(\"find 3\", left)
# return left
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
#首先排序
position.sort()
# print(position)
distance = [0 for _ in range(len(position)-1)]
for i in range(len(position)-1):
diff = position[i+1] - position[i]
distance[i] = diff
left = min(distance)
right = ceil((position[-1] - position[0]) / (m-1))
def check(diff, m):
m -= 1
pre_dis = 0
for i in range(0, len(distance)):
if distance[i]+pre_dis >= diff:
m -= 1
if m <= 0:
# print(diff, \"True\")
return True
pre_dis = 0
else:
pre_dis += distance[i]
# print(diff, \"False\")
return False
# print(\"left\", left, \"right\", right)
while left < right:
mid = (left + right+1) // 2
if check(mid, m) == True:
left = mid
# if left == right:
# print(\"find 1\", left)
# break
# if left + 1 == right:
# if check(right, position, m):
# left = right
# print(\"find 2\", left)
# break
else:
right = mid - 1
# print(\"find 3\", left)
return left
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
min_p, max_p = position[0], position[-1]
def enough(x):
prev, cnt = min_p, 1
for i, p in enumerate(position):
if p - prev >= x:
prev = p
cnt += 1
if cnt >= m:
return True
return False
l, r = 1, max_p - min_p
while l < r:
mid = (l + r + 1) // 2
if enough(mid):
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
if m == 2: return position[-1] - position[0]
lp = 0
rp = (position[-1] - position[0])
def can(gap):
lidx = 0
left = m - 1
ptr = 1
while left > 0 and ptr < n:
if position[ptr] - position[lidx] >= gap:
left -= 1
lidx = ptr
ptr = lidx + 1
continue
ptr += 1
return left == 0
ans = 0
while lp < rp:
mid = (lp+rp+1)//2
if can(mid):
lp = mid
else:
rp = mid-1
return lp
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def check_works(position, m, dist):
# placing ball 1
balls_to_place = m - 1
last_ball_pos = 0
for ind in range(last_ball_pos, len(position)):
# print(position[ind])
if position[ind] - position[last_ball_pos] >= dist:
balls_to_place -= 1
last_ball_pos = ind
if balls_to_place == 0:
break
if balls_to_place == 0:
return 1
else:
return 0
# let's try between 1 and 1000000000
position.sort()
lb = 1
rb = 1000000000
while (lb != rb - 1):
ret = check_works(position, m, int((lb + rb)/2))
if(ret == 0):
rb = int((lb + rb)/2)
else:
lb = int((lb + rb)/2)
print(lb,rb)
return lb
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
def count(d):
c, cur = 1, position[0]
for i in range(1, n):
if position[i] - cur >= d:
c += 1
cur = position[i]
return c
l, r = 0, position[-1] - position[0]
while l < r:
mid = r - (r - l) // 2
if count(mid) >= m:
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def possible(force):
prev = float('-inf')
balls_placed = 0
for x in position:
if x-prev < force:
continue
prev= x
balls_placed+=1
return(balls_placed>=m)
lo,hi = 0 , max(position)
while lo<hi:
mid = lo+hi+1>>1
if possible(mid):
lo=mid
else:
hi=mid-1
return(lo)
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
def check(dis):
num = 0
presum = 0
for i in range(1, n):
presum += position[i] - position[i-1]
if presum >= dis:
presum = 0
num += 1
return num
l, r = 0, position[-1] - position[0]
while l < r-1:
mid = (l + r) // 2
if check(mid) >= m-1:
l = mid
else:
r = mid - 1
return r if check(r) >= m-1 else l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
l = 1
r = (position[-1]-position[0])//(m-1)
#print(l,r)
while (l<r):
mid = (l+r)//2 +1
placed = 1
distance = 0
start = position[0]
for i in range(1,len(position)):
distance = position[i]-start
if distance >= mid:
placed +=1
distance = 0
start = position[i]
#print(placed)
if placed >=m:
#answer = mid #we placed all balls
l = mid
else:
r = mid -1
return(l)
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
# diffs = [position[i] - position[i-1] for i in range(1, n)]
n = len(position)
def check(target_min_force):
put_count = 1
cur_force = 0
for i in range(1, n):
cur_force += position[i] - position[i-1]
if cur_force >= target_min_force:
cur_force = 0
put_count += 1
if put_count == m:
return True
return False
l, r = -1, position[-1] - position[0] + 1
while r - l > 1:
mid = (l + r) // 2
if check(mid):
l = mid
else:
r = mid
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def guess(position, m, guess):
placed = 1
last = position[0]
i = 1
while i < len(position):
#print(position[i], last, placed, m)
if position[i] - last >= guess:
placed += 1
last = position[i]
if placed == m:
return True
i += 1
# print(\"final \", placed, m)
return placed >= m
position.sort()
l = 0
r = position[-1] - position[0]
# l: checked OK
# r: haven't check
while l < r:
mid = (l + r + 1) // 2
#print(l, mid, r)
if guess(position, m, mid):
#print(\"guess OK\", mid)
l = mid
else:
r = mid - 1
return l
#placed = 1, last = 1, i = 1
#
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
def checkForce(force, positions, m):
prev = positions[0]
i = 1
while m > 0 and i < len(positions):
while positions[i] - force < prev:
i += 1
if i >= len(positions): return False
m -= 1
prev = positions[i]
i += 1
return m <= 0
# print(checkForce(2, position, m - 1))
l, r = 0, position[-1] - position[0]
while l < r:
mid = r - (r - l) // 2
# force is acceptable, let's see if we can achieve a higher force
if checkForce(mid, position, m-1):
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def bs(mid, m):
pre = position[0]
m -= 1
for x in position:
if x - pre >= mid:
pre = x
m -= 1
if m == 0:
return True
if m == 0:
return True
else:
return False
l = 1
r = 10 ** 9
position.sort()
while l < r:
mid = l + (r - l) // 2
if bs(mid, m):
l = mid + 1
else:
r = mid
return l - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
def count(d):
res = 1
curr = position[0]
for i in range(1,n):
if position[i] - curr >= d:
res += 1
curr = position[i]
return res
l = 0
r = position[-1] - position[0]
while l < r:
mid = l + (r-l) // 2 + 1
if count(mid) >= m:
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
# binary search on answer
n = len(position)
position.sort()
left = 1
right = max(position)
while right - left > 1:
mid = (left + right) // 2
if self.ok(position, m, mid):
left = mid
else:
right = mid
if self.ok(position, m, right):
return right
elif self.ok(position, m, left):
return left
def ok(self, position, m, force):
# first one at the
n = len(position)
stack = [position[0]]
i = 0
while i < n:
if position[i] - stack[-1] >= force:
stack.append(position[i])
i += 1
return len(stack) >= m
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def distributable(n):
pos = position.copy()
balls = m
while pos and balls:
balls -= 1
np = pos[-1] - n
while pos and pos[-1] > np:
pos.pop()
return not balls
position = sorted(position)
lo, hi = 1, (position[-1] - position[0]) // (m -1) + 1
ans = lo
while lo < hi:
mid = lo + (hi - lo) // 2
if distributable(mid):
ans = mid
lo = mid + 1
else:
hi = mid
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
left = 1
right = (position[-1] - position[0]) // (m-1)
def placeable(gap):
count = 0
x = -gap
for n in position:
if n-x >= gap:
count +=1
x = n
if count == m:
return True
return False
while left < right:
mid = (left + right + 1) // 2
if placeable(mid):
left = mid
else:
right = mid - 1
return left
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position = sorted(position)
highest_f = int((position[-1] - position[0]) / (m - 1))
def helper(position, m, high_f, low_f, curr_f):
selected_pos = [position[0]]
for i in position:
if i - selected_pos[-1] >= curr_f:
selected_pos.append(i)
if high_f > low_f + 1:
if len(selected_pos) < m:
new_curr_f = int((low_f + curr_f) / 2)
return helper(position, m, curr_f, low_f, new_curr_f)
else:
new_curr_f = int((curr_f + high_f) / 2)
return helper(position, m, high_f, curr_f, new_curr_f)
else:
if len(selected_pos) < m:
return low_f
else:
return curr_f
return int(helper(position, m, highest_f, 1, highest_f))
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def c(p,m,d):
b=m-1
l=len(p)
acc=0
for i in range(1,l):
acc+=p[i]-p[i-1]
if acc>=d:
acc=0
b-=1
if b==0:
return True
return False
low=1
high=max(position)
mid=0
position.sort()
while low<=high:
mid=(low+high)//2
if c(position,m,mid):
low=mid+1
else:
high=mid-1
return high
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def distance(d):
p=0 # current position
c=1 # count of balls put into basket
while p<len(position)-1:
for i in range(p,len(position)):
if position[i]-position[p]>=d:
c+=1
break
p=i
if c==m:
return True
return False
l,r=0,position[-1]
while l<r:
md=r-(r-l)//2
#print(md,distance(md))
if distance(md):
l=md
else:
r=md-1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def verify(self, position: List[int], dis: int, balls: int) -> bool:
result = True
start = position[0]
cur = 1
counter = 1
while cur < len(position):
if position[cur] >= start + dis:
start = position[cur]
counter += 1
if counter >= balls:
return True
cur += 1
return False
def maxDistance(self, position: List[int], m: int) -> int:
result = 0
position = sorted(position)
minvalue = sys.maxsize
maxvalue = -sys.maxsize
for value in position:
minvalue = min(minvalue, value)
maxvalue = max(maxvalue, value)
left = 1
right = maxvalue - minvalue
while left <= right:
mid = left + (right - left) // 2
checked = self.verify(position, mid, m)
if checked:
left = mid + 1
result = mid
else:
right = mid - 1
return result
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from typing import List, Dict
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
a = position
a.sort()
M = m
def isok(m):
c = 0
prev = - (1 << 30)
for x in a:
if x - prev >= m:
c += 1
prev = x
return c >= M
l = 0
r = 10 ** 9
while l < r:
m = (l + r + 1) // 2
if isok(m):
l = m
else:
r = m - 1
return l
def _case(*a):
assert Solution().maxDistance(*a[:-1]) == a[-1]
pass
def test():
_case([1, 100], 2, 99)
_case([1,2,3,4,7], 3, 3)
_case([5,4,3,2,1,1000000000], 2, 999999999)
pass
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
from collections import deque
class Solution:
def helper(self, minforce, position, m):
#returns if minforce is valid or not
counter = 1
prev = position[0]
i = 1
while i < len(position) and counter < m:
if position[i] - prev < minforce:
i += 1
else:
counter += 1
prev = position[i]
i += 1
if counter == m:
return True
else:
return False
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
left = 1
right = position[-1] - position[0]
if m==2:
return right
while left < right:
mid = left + (right - left)//2
#print(mid)
if self.helper(mid, position, m):
left = mid + 1
else:
right = mid
return left-1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, A: List[int], m: int) -> int:
n = len(A)
A.sort()
def balls(d):
count, cur = 1, A[0]
for i in range(1, len(A)):
if A[i] - cur >= d:
count += 1
cur = A[i]
return count
l, r = 1, A[-1] - A[0]
while l < r:
mid = (l + r + 1) // 2
if balls(mid) >= m:
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
def num(dist):
count = 1
curr = position[0]
for i in range(1, n):
if position[i] - curr >= dist:
count += 1
curr = position[i]
return count
start = 0
end = position[-1] - position[0]
res = 0
while start < end:
mid = (start + end) // 2
if num(mid) >= m:
start = mid + 1
res = mid
else:
end = mid
if num(start) >= m:
return start
else:
return start - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def check(position,mid):
i=position[0]
count=0
for j in position:
if((j-i)>=mid):
count+=1
i=j
return count+1
l=0
position.sort()
r=position[-1]
while(r>(l+1)):
mid=math.ceil((l+r)/2)
print(mid)
k=check(position,mid)
if(k<m):
r=mid
else:
l=mid
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
l, r = 1, position[-1] - position[0] + 2
ans = 0
while l < r:
mid = (l+r+1) //2
cnt, i, j = 1, 1, 0
while i < len(position):
if position[i] - position[j] >= mid:
cnt += 1
j = i
i += 1
if cnt >= m:
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
position.sort()
def count(d):
ans, curr = 1, position[0]
for i in range(1, n):
if position[i] - curr >= d:
ans += 1
curr = position[i]
return ans
l, r = 0, position[-1] - position[0]
while l < r:
mid = (r - l + 1) // 2 + l
if count(mid) >= m:
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
left, right = 0, position[-1] - position[0]
def check(dist):
pre = position[0]
n = m - 1
i = 1
while n:
while i < len(position) and position[i] - pre < dist:
i += 1
if i >= len(position): return False
pre = position[i]
i += 1
n -= 1
return True
while left <= right:
mid = (left + right) // 2
if check(mid):
left = mid + 1
else:
right = mid - 1
return left - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def _MaxPossibleForce(self, position):
return position[-1] - position[0]
def _GetNumAssignedBallsForGivenForce(self, position, force):
i = 1
assigned = 1
# 0th item is the first ball
last_assigned_position = 0
while i < len(position):
if position[i] - position[last_assigned_position] >= force:
assigned += 1
last_assigned_position = i
i += 1
return assigned
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
max_force = self._MaxPossibleForce(position)
min_force = 1 # as m <= len(position)
while min_force <= max_force:
search_force = (min_force + max_force)//2
num_assigned = self._GetNumAssignedBallsForGivenForce(position, search_force)
if num_assigned < m:
max_force = search_force - 1
else:
min_force = search_force + 1
return max_force
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def check(position, d, m):
last = position[0]
balls = m - 1
i = 1
while balls and i < len(position):
if position[i] - last < d:
i += 1
else:
last = position[i]
balls -= 1
return balls == 0
position.sort()
hi = position[-1]
lo = 1
while lo < hi:
mi = (lo + hi + 1) // 2
if check(position, mi, m):
lo = mi
else:
hi = mi - 1
return lo
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
#829
class Solution:
def maxDistance(self, arr: List[int], m: int) -> int:
arr.sort()
low = 1
high = arr[-1] - arr[0]
def place(limit):
prev = -1e10
j = 0
for i in range(m):
while j < len(arr) and arr[j] - prev < limit:
j += 1
if j == len(arr):
return False
prev = arr[j]
j += 1
return True
while low <= high:
cur = int((low + high) / 2)
#print(low, high, cur, place(cur))
if place(cur):
low = cur + 1
else:
high = cur - 1
return high
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def helper(d):
cnt = 1
cur = position[0]
for i in range(1, len(position)):
if cur + d <= position[i]:
cur = position[i]
cnt += 1
return cnt
l, r = 0, position[-1] - position[0]
res = -float('inf')
while l <= r:
# print(l, r)
# if l == r and helper(l) == m:
# return l
mid = (l + r) >> 1
if helper(mid) == m:
res = max(mid, res)
l = mid + 1
elif helper(mid) > m:
# res = max(mid, res)
l = mid + 1
else:
r = mid - 1
return max(res, l-1)
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def check(threshold):
last = position[0]
count = 1
for p in position[1:]:
if p - last >= threshold:
count += 1
last = p
if count == m:
return True
return False
# binary search for max distance, l, r indicates distance
position.sort()
l, r = 1, position[-1]-position[0]
while l <= r:
mid = l + (r-l)//2
if check(mid):
l = mid + 1
else:
r = mid - 1
return r
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
# m表示球的数量
# 寻找m-1个空隙,来满足距离要求
def dist_valid(interval, k):
count,k_count = 0,0
for i in interval:
count += i
if count>=k:
count = 0
k_count += 1
if k_count>=m-1:
return True
return False
interval = []
sort_pos = sorted(position)
for i in range(len(sort_pos)-1):
interval.append(sort_pos[i+1]-sort_pos[i])
left = 1
right = (max(sort_pos)-min(sort_pos))//(m-1)
while left<right:
mid = right - (right-left)//2
if dist_valid(interval,mid):
left = mid
else:
right = mid - 1
return left
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
position.sort()
def count(d):
ans, cur = 1, position[0]
for i in range(1, n):
if position[i] - cur >= d:
ans += 1
cur = position[i]
return ans
l, r = 0, position[-1] - position[0]
while l < r:
mid = l + (r - l + 1)// 2
if count(mid) >= m:
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
import bisect
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
def possible(gap):
prev = position[0]
idx = 0
left = m - 1
while left > 0:
idx = bisect.bisect_left(position, prev+gap, idx + 1, n)
if idx >= n:
return False
else:
prev = position[idx]
left -= 1
return True
position.sort()
min_gap = 1
max_gap = position[-1] - position[0]
while min_gap <= max_gap:
cur_gap = (min_gap + max_gap) // 2
if possible(cur_gap):
# print(\"possible\", cur_gap)
min_gap = cur_gap + 1
else:
# print(\"impossible\", cur_gap)
max_gap = cur_gap - 1
return min_gap - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
lo = 1
hi = position[-1] - position[0]
def count(dis):
res = 1
i = 0
curr = 0
while i < len(position):
if position[i]-position[curr] >= dis:
res += 1
curr = i
i += 1
return res
# def count(d):
# ans, curr = 1, position[0]
# for i in range(1, len(position)):
# if position[i] - curr >= d:
# ans += 1
# curr = position[i]
# return ans
while lo < hi:
mid = hi-(hi-lo)//2
if count(mid) >= m:
lo = mid
else:
hi = mid-1
return lo
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
left,right = 0,position[-1]-position[0] # search space is sorted
def count_balls(force):
balls,prev = 1,position[0]
for cur in position[1:]:
if cur-prev >= force:
balls += 1
prev = cur
return balls
while left < right:
# mid = left + (right-left)//2
mid = right - (right-left) // 2
# mid = (left+right+1)//2
if count_balls(mid) >= m:
left = mid
else:
right = mid - 1
return left
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def is_possible(threshold):
count = 0
prev = -math.inf
for x in position:
if x - prev >= threshold:
count += 1
prev = x
return count >= m
span = position[-1] - position[0]
lo, hi = 0, span
while lo < hi:
mid = (lo + hi + 1) // 2
if is_possible(mid):
lo = mid
else:
hi = mid - 1
return lo
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
# min-max, dp?
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def feasible(dist):
placed, pos = 0, 0
prev = float('-inf')
while pos < len(position):
if position[pos] - prev >= dist: # can place one more
placed += 1
prev = position[pos]
pos += 1
if placed == m:
return True
return False
left, right = 1, position[-1]
while left < right:
dist = (left + right) // 2
if feasible(dist):
left = dist + 1
else:
right = dist
return left - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position, m):
position = sorted(position)
start, end = 1, position[-1] - position[0]
while start + 1 < end:
mid = start + (end - start) // 2
if self.check(position, m, mid):
start = mid
else:
end = mid
if self.check(position, m, end):
return end
if self.check(position, m, start):
return start
def check(self, position, m, delta):
n = len(position)
i = 1
j = 1
last = position[0]
while i <= m - 1 and j <= n - 1:
if position[j] >= last + delta:
last = position[j]
i += 1
j += 1
return i == m
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.