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:
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 maxDistance(self, position: List[int], m: int) -> int:
def helper(distance):
balls = m - 1
previous = position[0]
for i in position[1:]:
if i - previous >= distance:
balls -= 1
if balls == 0:
return True
previous = i
return False
position.sort()
min_distance = 1
max_distance = (position[-1] - position[0]) // (m - 1)
# max_distance = position[-1] - position[0]
result = 1
while min_distance <= max_distance:
mid = (min_distance + max_distance) // 2
if helper(mid):
result = mid
min_distance = mid + 1
else:
max_distance = 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
|
import bisect
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def is_ok(x):
cur = -x
for i in range(m):
j = bisect.bisect_left(position, cur+x)
if 0 <= j < len(position):
cur = position[j]
else:
return False
else:
return True
l = 0
r = 10**18
while l+1 < r:
c = (l+r)//2
if is_ok(c):
l = c
else:
r = c
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 - (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
|
import math
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position = sorted(position)
max_possible_answer = (position[-1] - position[0]) // (m-1) #3
# linear search - time limit exceed
# for i in range(1,1+max_possible_answer):
# if not self.is_possible( position, m, i):
# break
# return i-1
# binary search
l = 1
r = max_possible_answer + 1
# T T T T T F F -> want to know last T
# similar to 278
while l < r :
# why + 1?? because we use l = mid (inclusive) if we don't round up
# it will get stuck
# mid = (r+l) // 2 + 1
mid = math.ceil((r+l)/2)
if self.is_possible( position, m, mid):
l = mid # mid may be answer
else:
r = mid-1 # mid can't be answer
return l
def is_possible(self, position, m, gap):
i = 1
prev = position[0]
m -= 1
while i < len(position) and m > 0:
if position[i] - prev >= gap:
m -= 1
prev = position[i]
else:
i += 1
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:
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
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 getBallCount(position, force):
res, prev = 1, 0
for i in range(1, len(position)):
if position[i] - position[prev] >= force:
res += 1
prev = i
return res
# position.sort()
# l,r,ans=0,position[-1],0
# while l<=r:
# gap=l+(r-l)//2
# if getBallCount(position, gap) >= m:
# ans=gap
# l=gap+1
# else:
# r=gap-1
# return ans
position.sort()
if m == 2:
return position[-1] - position[0]
l, r, res = 0, position[-1] + 1, 0
while l < r:
mid = l + (r - l) // 2
# force is too small
if getBallCount(position, mid) >= m:
l = mid + 1
res = mid
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:
position.sort()
left = 0
right = position[-1] - position[0]
best = 0
while left <= right:
target = left + (right-left)//2
if self.solve(position, m, target):
left = target+1
best = max(best, target)
else:
right = target-1
return best
def solve(self, position, m, target):
p = position[0]
m -= 1
for p2 in position[1:]:
if p2 - p >= target:
m -= 1
p = p2
if m == 0:
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: List[int], m: int) -> int:
position.sort()
l = 0
r = (position[-1]-1) // (m-1) * 2
while l < r:
intrv = (l+r) // 2
c = 0
prev = float('-inf')
for p in position:
if p >= prev + intrv:
c += 1
prev = p
if c >= m:
best = intrv
l = intrv + 1
else:
r = intrv
return best
|
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)
max_possible_answer = (position[-1] - position[0]) // (m-1) #3
# linear search - time limit exceed
# for i in range(1,1+max_possible_answer):
# if not self.is_possible( position, m, i):
# break
# return i-1
# binary search
l = 1
r = max_possible_answer + 1
# T T T T T F F -> want to know last T
# similar to 278
while l < r :
# why + 1?? because we use l = mid (inclusive) if we don't round up
# it will get stuck
mid = (r+l) // 2 + 1
if self.is_possible( position, m, mid):
l = mid # mid may be answer
else:
r = mid-1 # mid can't be answer
return l
def is_possible(self, position, m, gap):
i = 1
prev = position[0]
m -= 1
while i < len(position) and m > 0:
if position[i] - prev >= gap:
m -= 1
prev = position[i]
else:
i += 1
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:
def check(position,val,m):
balls = 1
prev = position[0]
for i in range(1,len(position)):
if (position[i]-prev) >= val:
prev = position[i]
balls += 1
if balls >= m:
return True
return False
position.sort()
lo,hi = 0 , position[-1]-position[0]
while lo < hi:
mid = (hi+lo+1)//2
if check(position,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()
left, right = 1, position[-1] - position[0]
while left <= right:
mid = left + (right - left)//2
num_balls = self.count(position, m, mid)
if num_balls > m: # too many balls can fit in the given baskets, force is too small, search the right hal
left = mid + 1
elif num_balls == m: # need to find the maximum force, continue to search the right half
left = mid + 1
else:
right = mid -1
return left -1
def count(self, position, m, mid):
prev = position[0]
m =1
for i in position[1:]:
# if i - mid == prev:
if i - prev >=mid:
m+=1
prev = i
return 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:
# https://leetcode.com/problems/magnetic-force-between-two-balls/discuss/803580/Python-Solution-or-Binary-Search
def maxDistance(self, position: List[int], m: int) -> int:
def getBallCount(position, force):
# res starts from 1, since we put one ball at index = 0.
res, prev = 1, 0
for i in range(1, len(position)):
if position[i] - position[prev] >= force:
res += 1
prev = i
return res
position.sort()
if m == 2:
return position[-1] - position[0]
l, r, res = 0, position[-1] + 1, 0
while l < r:
mid = l + (r - l) // 2
# force is too small
if getBallCount(position, mid) >= m:
l = mid + 1
res = mid
else:
r = mid
# return res, not l.
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
|
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 + 1) // 2
prev = position[0]
idx = 0
left = m - 1
res = True
while left > 0:
idx = bisect.bisect_left(position, prev + cur_gap, idx + 1, n)
if idx >= n:
res = False
break
else:
prev = position[idx]
left -= 1
if res:
min_gap = cur_gap
else:
max_gap = cur_gap - 1
return min_gap
|
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 helper(d):
ans, curr = 1, position[0]
for i in range(1, n):
if position[i] - curr >= d:
ans += 1
curr = position[i]
return ans
left, right = 0, position[-1] - position[0]
while left <= right:
mid = left + (right - left) // 2
if helper(mid) < m:
right = mid - 1
else:
left = 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 maxDistance(self, position: List[int], m: int) -> int:
def getBallCount(position, force):
res, prev = 1, 0
for i in range(1, len(position)):
if position[i] - position[prev] >= force:
res += 1
prev = i
return res
# position.sort()
# l,r,ans=0,position[-1],0
# while l<=r:
# gap=l+(r-l)//2
# if getBallCount(position, gap) >= m:
# ans=gap
# l=gap+1
# else:
# r=gap-1
# return ans
position.sort()
if m == 2:
return position[-1] - position[0]
l, r, res = 0, position[-1] + 1, 0
while l < r:
mid = l + (r - l) // 2
# force is too small
if getBallCount(position, mid) >= m:
l = mid + 1
res = mid
else:
r = mid
# return res, not l.
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:
# https://leetcode.com/problems/magnetic-force-between-two-balls/discuss/803580/Python-Solution-or-Binary-Search
def maxDistance(self, position: List[int], m: int) -> int:
def getBallCount(position, force):
# res starts from 1, since we put one ball at index = 0.
res, prev = 1, 0
for i in range(1, len(position)):
if position[i] - position[prev] >= force:
res += 1
prev = i
return res
position.sort()
if m == 2:
return position[-1] - position[0]
l, r, res = 0, position[-1] + 1, 0
while l < r:
mid = l + (r - l) // 2
# force is too small
if getBallCount(position, mid) >= m:
l = mid + 1
# record res
res = mid
else:
r = mid
# return res, not l.
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()
def possible(force):
prev = float('-inf')
balls_placed = 0
for x in position:
if x-prev >= force:
prev= x
balls_placed+=1
return(balls_placed>=m)
lo,hi = 0, max(position)
while lo<hi:
mid = (lo+hi+1)//2
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:
def valid(position, force):
prev, j = 0, 1
for i in range(1, m):
while j < len(position) and position[j] - position[prev] < force:
j += 1
if j == len(position):
return False
prev = j
return True
def getBallCount(position, force):
res, prev = 1, 0
for i in range(1, len(position)):
if position[i] - position[prev] >= force:
res += 1
prev = i
return res
# position.sort()
# l,r,ans=0,position[-1],0
# while l<=r:
# gap=l+(r-l)//2
# if getBallCount(position, gap) >= m:
# ans=gap
# l=gap+1
# else:
# r=gap-1
# return ans
position.sort()
if m == 2:
return position[-1] - position[0]
l, r, res = 0, position[-1] + 1, 0
while l < r:
mid = l + (r - l) // 2
# force is too small
if getBallCount(position, mid) >= m:
l = mid + 1
res = mid
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:
position.sort()
def is_valid(guess):
prev = position[0]
balls = 1
for p in position[1:]:
if p-prev >= guess:
prev = p
balls += 1
if balls == m:
return True
return False
l, r = 1, (position[-1]-position[0]) // (m-1)
while l < r:
guess = (l + r + 1) >> 1
if is_valid(guess):
l = guess
else:
r = guess-1
return l
# position.sort()
# l, r = 1, position[-1] - position[0]
# while l < r:
# cand = (l + r + 1) >> 1
# if self.is_valid(position, m, cand):
# l = cand
# else:
# r = cand - 1
# return l
#
#def is_valid(self, position, m, cand):
# prev = position[0]
# rem = m - 1
# for pos in position[1:]:
# if pos >= prev + cand:
# prev = pos
# rem -= 1
# if rem == 0:
# 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: 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[n-1]-position[0]
while l < r:
mid = (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
|
class Solution:
# https://leetcode.com/problems/magnetic-force-between-two-balls/discuss/803580/Python-Solution-or-Binary-Search
def maxDistance(self, position: List[int], m: int) -> int:
def getBallCount(position, force):
res, prev = 1, 0
for i in range(1, len(position)):
if position[i] - position[prev] >= force:
res += 1
prev = i
return res
position.sort()
if m == 2:
return position[-1] - position[0]
l, r, res = 0, position[-1] + 1, 0
while l < r:
mid = l + (r - l) // 2
# force is too small
if getBallCount(position, mid) >= m:
l = mid + 1
res = mid
else:
r = mid
# return res, not l.
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:
if len(position) < m:
return 0
def valid_force(f):
ball_pos = 0
balls = m - 1
for i in range(1, len(position)):
if position[i] - position[ball_pos] >= f:
balls -= 1
ball_pos = i
if balls == 0:
return True
return False
position.sort()
l, h = 1, position[-1]
while l < h:
f = l + (h - l + 1) // 2
if valid_force(f):
l = f
else:
h = 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 is_force_valid(self, position: List[int], m: int, force: int) -> bool:
last_pos = position[0]
m -= 1
for i in range(1, len(position)):
if position[i] - last_pos >= force:
m -= 1
if m == 0:
return True
last_pos = position[i]
return False
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
left = 1
right = position[-1] - position[0]
while right > left:
mid = (right + left + 1) // 2
if self.is_force_valid(position, m, 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:
# def pd(i, m):
# if m == 1:
# return float(\"inf\")
# if memo[i][m]:
# return memo[i][m]
# res = 0
# for j in range(i+1, len(position)):
# res = max(res, min(position[j] - position[i], pd(j, m-1)))
# memo[i][m] = res
# return res
# position = sorted(position)
# memo = [(1 + m) * [0] for _ in range(1 + len(position))]
# res = pd(0, m)
position = sorted(position)
if m == 2:
return position[-1] - position[0]
def solve(threshold, m):
last_ball_pos = position[0]
for pos in position[1:]:
if pos - last_ball_pos >= threshold:
m -= 1
last_ball_pos = pos
if m == 0:
return True
return False
start = 0
end = position[-1] - position[0]
res = 0
while start <= end:
middle = (start + end) // 2
if solve(middle, m-1):
start = middle + 1
res = max(res, middle)
else:
end = middle - 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, 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
last = position[0]
for j in range(1, n):
if position[j] >= last + delta:
last = position[j]
i += 1
if i == m:
return True
return i == 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:
position.sort()
def possible(force):
prev = float('-inf')
balls_placed = 0
for x in position:
if x-prev >= force: # then that means we have that required force and we can keep a new ball here at position x
prev= x
balls_placed+=1
if balls_placed ==m: # if at any moment we are able to place m balls, we stop and return True.
return(True)
return(False)
lo,hi = 0, max(position)
while lo<hi:
mid = (lo+hi+1)//2
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:
def check(x: int):
pre = position[0]
num = 1
for ii in range(1, N):
if position[ii] - pre >= x:
pre = position[ii]
num += 1
return num >= m
N = len(position)
position = sorted(position)
left, right, res = 0, position[-1] - position[0], -1
while left <= right:
mid = (left + right) >> 1
if check(mid):
res = mid
left = mid + 1
else:
right = 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 = sorted(position)
s = 1
e = (position[-1]-position[0]) // max(m-1, 1) + 1
def check(mid) :
t = position[0]
ret_val = 1
for k in position :
if ret_val == m :
return True
if k - t >= mid :
ret_val += 1
t = k
return ret_val == m
while e > s + 1 :
mid = (e+s)//2
if check(mid) :
s = mid
else :
e = mid
return s
|
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 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:
# sort balls
position.sort()
# number of balls that can be placed while the difference of two balls is at least diff
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]
# try out values of m using binary search on left and right
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
|
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:
curr, ans = position[i], ans + 1
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
def maxDistancwe(self, position: List[int], m: int) -> int:
def count(mid):
r, c = 1, position[0]
for i in range(1,len(position)):
if position[i]-c >= mid:
c, r = position[i], r + 1
return r
position.sort()
l, r = 0, position[-1] - position[0]
while l < r:
mid = (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:
n = len(position)
position.sort()
def isfail(mid):
ans = 1
curr = position[0] # alike greedy idea, we just put the first ball at position 0!
for i in range(1, n):
if position[i] - curr >= mid:
ans += 1
curr = position[i]
return ans < m
left = 0 # 注意,不应该把left赋值为min(position)! 因为我们求得是distance!
# note that, left = min(position) is wrong, since we are looking for 'distance'!
right = max(position) - min(position) + 1
while left < right:
mid = left + (right - left) // 2
if isfail(mid):
right = mid
else:
left = mid + 1
return left - 1 # left is the min value to fail, so 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()
res = 0
l, r = 1, (position[-1] - position[0]) // (m - 1)
while l <= r:
mid = (l + r) // 2
prev = position[0]
ball = 1
for i in position[1:]:
if i - prev >= mid:
ball += 1
prev = i
if ball == m:
break
if ball == m:
l = mid + 1
res = mid
else:
r = 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, pos: [int], m: int) -> int:
pos.sort()
N = len(pos)
high = (pos[-1]-pos[0])//(m-1)
low = 1
def check(dist):
cnt = 1
loc = pos[0]
for i in range(1, N):
if pos[i]-loc>=dist:
cnt += 1
loc = pos[i]
if cnt == m:
return True
return False
while high!=low:
mid = (high+low+1)//2
if check(mid):
low = mid
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:
N = len(position)
A = position
A.sort()
lo, hi = 1, A[-1] - A[0]
while lo <= hi:
mid = (lo + hi + 1) // 2
prev = A[0]
left = m - 1
for i in range(1, N):
if A[i] - prev >= mid:
prev = A[i]
left -= 1
if left == 0:
break
if left == 0:
answer = mid
lo = mid + 1
else:
hi = 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
|
def solve(positions, m, target):
p = positions[0]
m -= 1
for p2 in positions[1:]:
if p2 - p >= target:
m -= 1
p = p2
if m == 0:
return True
return False
class Solution:
def maxDistance(self, positions: List[int], m: int) -> int:
positions.sort()
l, r = 0, positions[-1] - positions[0]
best = 0
while l <= r:
target = (l + r) // 2
if solve(positions, m, target):
l = target + 1
best = max(best, target)
else:
r = target - 1
return best
|
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 countBalls(d):
n_balls, cur = 1, position[0]
for i in range(1, n):
if position[i] - cur >= d:
n_balls += 1
cur = position[i]
return n_balls
left = 1
right = position[n-1] - position[0]
while left <= right:
mid = left + (right - left) // 2
if countBalls(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.sort()
def ifvalid(diff,num_division):
selected=0
cur=position[0]
for i,f in enumerate(position):
if selected>=num_division:
return True
if (f-cur)>=diff:
selected+=1
cur=position[i]
else:
if i==len(position)-1:
return False
return selected>=num_division
def search(l,r):
while l<r:
mid=l+(r-l)//2
if not ifvalid(mid,m-1):
r=mid
else:
l=mid+1
return l-1
max_diff=(position[-1]-position[0])//(m-1) +1
return search(1,max_diff+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 count(d):
ans = 1
pre_pos = position[0]
for i in range(1, n):
if position[i] - pre_pos >= d:
ans += 1
pre_pos = position[i]
return ans
n = len(position)
position.sort()
left = 1
right = position[-1] - position[0]
while left < right:
mid = left + (right-left+1) // 2
if count(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 count_d(dis):
ans, cur = 1, position[0]
for i in range(1, len(position)):
if position[i] - cur >= dis:
ans += 1
cur = position[i]
return ans
l, r = 0, position[-1] - position[0]
while l < r:
mid = r - (r-l)//2
if count_d(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 is_possible(gap,left):
prev=position[0]
for i in range(1,len(position)):
if position[i]-prev>=gap:
prev=position[i]
left-=1
if not left: return True
return False
position.sort()
l,r,ans=0,position[-1],0
while l<=r:
gap=(r+l)//2
if is_possible(gap,m-1):
ans=gap
l=gap+1
else:
r=gap-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 is_possible(gap,left):
prev=position[0]
for i in range(1,len(position)):
if position[i]-prev>=gap:
prev=position[i]
left-=1
if not left: return True
return False
position.sort()
l,r,ans=0,position[-1],0
while l<=r:
gap=l+(r-l)//2
if is_possible(gap,m-1):
ans=gap
l=gap+1
else:
r=gap-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:
# 最大间隙使用二分法查找
# 检查最大间隙的合理性
# 从一堆间隙中,寻找连续长度为dist的m个间隙
interval = []
sort_pos = sorted(position)
for i in range(len(sort_pos)-1):
interval.append(sort_pos[i+1]-sort_pos[i])
def is_valid(dist,interval):
ball = 1
count = 0
for i in interval:
count += i
if count>=dist:
ball += 1
count = 0
if ball>=m:
return True
return False
left = 1
right = sum(interval)//(m-1)
# 如果最优值停在right上,那么这个循环永远不能结束
while left<right:
mid = right - (right-left)//2
if is_valid(mid,interval):
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 fit_balls(self, positions, n, min_dist):
last_position = positions[0]
placement_count = 1
for p in positions:
if p - last_position >= min_dist:
last_position = p
placement_count += 1
if placement_count >= n:
return True
return False
def maxDistance(self, position: List[int], m: int) -> int:
pos = sorted(position)
lo = 0
hi = pos[-1]-pos[0]
while lo < hi:
mid = hi - (hi-lo) // 2
can_fit = self.fit_balls(pos, m, mid)
if not can_fit:
hi = mid-1
else:
lo = mid
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 valid(force):
nonlocal position, m
start = count = 0
for index, value in enumerate(position):
if value - force >= position[start]:
count += 1
start = index
return count >= m - 1
lo, hi = 1, position[-1] - position[0]
while lo < hi:
mid = lo + (hi - lo + 1) // 2
if valid(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, positions: List[int], m: int) -> int:
positions.sort()
def possible(positions,m,v):
cur=0
for i in range(1,len(positions)):
if positions[i]-positions[cur]>=v:
m-=1
cur=i
if m==1: return True
return m<=1
left,right=1,positions[-1]-positions[0]
ans=0
while left<=right:
v=(left+right)//2
# print(possible(positions,m,v),m,v)
if possible(positions,m,v):
ans=v
left=v+1
else:
right=v-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
|
from math import ceil
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
minPos, maxPos = position[0], position[len(position) - 1]
lo, hi = 0, maxPos - minPos
curAns = -1
while lo <= hi:
mid = (lo + hi) // 2
if self.canFitAllBalls(mid, m, position):
curAns = max(curAns, mid)
lo = mid + 1
else:
hi = mid - 1
return curAns
def canFitAllBalls(self, dist, m, position):
prev = None
for pos in position:
if prev == None or (pos - prev >= dist):
m -= 1
prev = pos
if m == 0:
break
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:
positions = position
positions.sort()
n = len(positions)
def f(d):
prev = 0
balls = 1
for i in range(1, n):
if positions[i] - positions[prev] >= d:
prev = i
balls += 1
if balls >= m:
return True
return False
lo, hi = 0, positions[-1] - positions[0]
while lo < hi:
mi = lo + (hi - lo + 1) // 2
if f(mi):
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
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
''' DP causing timeout
def get_max_force(pos, count):
#print(f\"get_max_force pos_index {pos} posistion {position[pos]} count {count}\")
if count <=1 :
return float('inf')
if dp[pos][count-1] > -1:
#print(\"saved time\")
return dp[pos][count-1]
if n - pos == count:
min_force = float('inf')
for pos_index in range(pos+1, n):
min_force = min(min_force, position[pos_index]-position[pos_index-1])
#print(f\"dp 3 pos_index {pos} position {position[pos]} count{count} : {dp[pos][count-1]}\")
dp[pos][count-1] = min_force
return min_force
if count == 2:
dp[pos][count-1] = position[-1] - position[pos]
#print(f\"dp 2 pos_index {pos} position {position[pos]} position[-1] {position[-1]} count{count} : {dp[pos][count-1]}\")
return dp[pos][count-1]
max_force = 0
for pos_index in range(pos+1, n):
if n - pos_index < count - 1:
break
max_force = max(max_force, min(position[pos_index]-position[pos], get_max_force(pos_index, count-1)))
dp[pos][count-1] = max_force
#print(f\"dp 1 pos_index {pos} position {position[pos]} count{count} : {max_force}\")
return max_force
n = len(position)
position.sort()
dp = [[-1] * m for _ in position]
if m == n:
return get_max_force(0, m)
if m == 2:
return position[-1] - position[0]
max_force = 0
for pos_index in range(0, n):
max_force = max(max_force, get_max_force(pos_index, m))
return max_force
'''
n = len(position)
position.sort()
def calc(d):#return number of balls while maintaining distance d
#print(f\"calc {d}\")
curr = position[0]
ans = 1
for i in range(1, n):
if position[i] - curr >= d:
index = i
curr = position[i]
ans += 1
#print(f\"return {ans}\")
return ans
l, r = 0, position[-1] - position[0]
#d = r
d = 0
while l < r:
#mid = (r + l + 1) // 2
#print(f\"l {l} r{r} d {mid}\")
mid = l + (r-l) // 2
count = calc(mid)
if count < m:
r = mid
else:
l = mid + 1
'''
if count < m:
r = mid - 1
elif count >= m:
l = mid
'''
#print(l, r)
#print(f\"{calc(l)}\")
#print(f\"{calc(l-1)}\")
if calc(l) == m:
return l
else:
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:
n= len(position)
position.sort()
if m==2:
return position[-1]- position[0]
cache={}
def F(x):
placed=1
prev= position[0]
for i in range(1, n):
if position[i]- prev>= x:
placed+=1
prev= position[i]
if placed== m:
return True
return False
def get_x():
l= 1
r= position[n-1]- position[0]
while l<=r:
mid= (l+r)//2
if mid not in cache:
cache[mid]= F(mid)
c= cache[mid]
if c:
l= mid+1
ans= mid
else:
r=mid-1
return ans
return (get_x())
|
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 canPlace(force):
balls = 1
curr_dist = curr_i = 0
for i in range(1, len(position)):
curr_dist = position[i] - position[curr_i]
if curr_dist >= force:
curr_i = i
curr_dist = 0
balls += 1
return balls
position.sort()
left, right = 0, position[-1] - position[0]
while left < right:
mid = (left + right + 1) // 2
if canPlace(mid) < m:
right = mid - 1
else:
left = mid
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()
diff = position[-1] - position[0]
right = diff // (m - 1)
left = 1
def condition(num):
k = m - 1
prev = position[0]
for n in position[1:]:
if n - prev >= num:
k -= 1
prev = n
if k == 0:
return True
return False
while left < right:
mid = left + (right - left) // 2
# print(mid, condition(mid))
if condition(mid):
left = mid + 1
else:
right = mid
if condition(left):
return left
else:
return left - 1
# print(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:
# dp 一定超时
#minimax的题目,基本上都是binary search + greedy helper function
# 用count(d)表示:当最小的距离是d时,最多能够放的球的个数,目标就是去找count(d)==m时,最大的d
# 用二分思想:
# 当count(d)<m时,说明d太大了,有的球放不下
# 当count(d)>m时,说明d太小了,还有更多的球能放
position.sort()
l = 1
r = position[-1] - position[0]
while l < r:
mid = r - (r-l)//2
if self.helper(mid, position) >= m:
l = mid
else:
r = mid-1
return l
def helper(self, d, position):
res = 1
cur = position[0]
for i in range(1, len(position)):
if position[i] - cur >= d:
cur = position[i]
res += 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()
if m == 2: return position[-1] - position[0]
l, r = 1, position[-1]
ans = 0
while l < r:
mid = (l + r) // 2
prev, balls = -1000000000, 0
for p in position:
if p - prev >= mid:
balls += 1
if balls == m: break
prev = p
if balls == m:
ans = mid
l = mid + 1
else:
r = 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:
def count(d):
res, prev=1, 0
for i in range(1, n):
if position[i]-position[prev]>=d:
prev=i
res+=1
return res
n=len(position)
position.sort()
l, r=1, position[-1]-position[0]
while l<r:
mid=(l+r)//2+1
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:
n = len(position)
position = sorted(position)
low, high = 1, position[-1]
ans = 0
while low <= high:
mid = low + (high - low) // 2
prev = position[0]
cnt = 1
for pos in position[1:]:
if (pos - prev) >= mid:
cnt += 1
prev = pos
if cnt >= m:
ans = max(ans, mid)
low = mid + 1
else:
high = 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:
# 1 2 3 4 5 6 7
# | | | | |
# b b b
# forces: 3, 3, 6
# results = 3
# 1 2 3 4 5 ...... 100000000 m = 2
# | | | | | |
# b b
# result = 100000000 - 1
# 1 2 3 4 5 ...... 100 m = 3
# | | | | | |
# b b
# mid = 50
# distance = 0 1 2 3 4 99
#
# mid = 25
# distance = 0 1 2 3 4 99
# mid = 11
# distance = 0 1 2 3 4 99
# mid = 5
# distance = 0 1 2 3 4 99
# mid = 4
# distance = 0 1 2 3 4 99
# 1 2 3 4 5 ...... 100 m = 4
# | | | | | |
# b b
# mid = 50
# distance = 0 1 2 3 4 99
#
# mid = 25
# distance = 0 1 2 3 4 99
# mid = 12
# distance = 0 1 2 3 4 99
# mid = 6
# distance = 0 1 2 3 4 99
# mid = 3
# distance = 1 2 3 1 96
# mid = 1
# mid = 2
def find_distance(mid_pos):
count = 1
pre = position[0]
for i in range(1, len(position)):
distance = position[i] - pre
if distance >= mid_pos:
count += 1
pre = position[i]
return count
position = sorted(position)
lo = 0
hi = position[-1]
while lo <= hi:
mid = lo + (hi-lo)//2
if find_distance(mid) >= m:
lo = mid + 1
else:
hi = mid - 1
return hi
|
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:
# 小心地使用暴力法求解: 可以尝试所有可能的间距, 找到最大的值
# 两个观察:
# 1. 要达到最优解第一个球一定放在第一个篮子里
# 2. 如果最小间距为x时无法摆下所有球, 那么比x大的间距(x+1, x+2...)也不可能摆下所有球
# 使用二分查找, 搜索能摆下所有球的间距
# 需要确定: 二分查找的范围, 要满足的条件是啥
# 二分查找模板->cond(k)成立的情况下最大化k
position.sort()
lo = 1
# 显然最大间距不会超过这个值
hi = position[-1] - position[0]
while lo < hi:
# 往右偏移的mid(corner case: lo=3, hi=4)
mid = (lo + hi + 1) // 2
if self.check(position, mid, m):
lo = mid
else:
# mid不能满足条件
hi = mid - 1
# 当退出循环时hi是满足条件的最大值
return hi
# 检查最小间距为x时是否能放下所有的球
def check(self, pos, x, m):
remain_ball = m - 1 # 第一个球就放在第一个篮子里了
n = len(pos)
prev = 0
for i in range(1, n):
if pos[i] - pos[prev] >= x:
remain_ball -= 1
prev = i
if remain_ball == 0:
return True
else:
continue
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: List[int], m: int) -> int:
N = len(position)
A = sorted(position)
max_gap = A[-1] - A[0]
min_gap = min(A[i] - A[i-1] for i in range(1, N))
if m == 2:
return max_gap
if m == N:
return min_gap
def check(min_dist):
prev = A[0]
left = m - 1
for i in range(1, N):
if A[i] - prev >= mid:
prev = A[i]
left -= 1
if left == 0:
return True
return False
lo, hi = min_gap, max_gap
while lo < hi:
mid = (lo + hi + 1) // 2
c = check(mid)
if c:
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()
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, int(math.ceil((position[-1] - position[0])/(m-1)))
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, 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 - 2:
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, arr: List[int], m: int) -> int:
arr = sorted(arr)
small = sys.maxsize
for a, b in zip(arr, arr[1:]):
small = min(small, b-a)
def count(d):
cur = arr[0]
res = 1
for i in range(1, len(arr)):
if arr[i] - cur >= d:
res += 1
cur = arr[i]
return res >= m
def bs(l, r):
if l > r: return 0
mid = l + (r-l)//2
if count(mid):
return bs(mid+1, r) or mid
else:
return bs(l, mid-1)
return bs(small, arr[-1]-arr[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()
w = (position[-1] - position[0]) // (m-1)
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 possible(force):
prev = float('-inf')
balls_placed = 0
for x in position:
if x-prev >= force: # then that means we have that required force and we can keep a new ball here at position x
prev= x
balls_placed+=1
return(balls_placed>=m)
lo,hi = 0, max(position)
while lo<hi:
mid = (lo+hi+1)//2
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:
def check(position, m, target):
ct = 1
j = 0
k = 1
flag = False
while k < len(position) and ct < m:
if position[k] -position[j] >= target:
ct += 1
if not flag:
kk = k
flag = True
j = k
k += 1
return (ct==m), kk
position = sorted(position)
max_p = position[-1]
i = position[0]
if m == 2:
return max_p - i
else:
target = (max_p-i)//(m-1)
ct = 1
b, kk = check(position, m, target)
if not b:
while not b and kk!=1:
target = position[kk] - i
target2 = position[kk-1] - i
b, kk = check(position, m, target2)
if not b:
left = 1
else:
left = position[kk] - i
right = target
while left + 1 < right:
target = (left + right)//2
if check(position, m, target)[0]:
left = target
else:
right = target
target = left
return target
|
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 put(self, position, d, m):
n = 1
curr = position[0]
for i in range(1, len(position)):
if position[i] - curr >= d:
n += 1
curr = position[i]
if n >= m:
return 1
return 0
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
low, high = 0, position[-1]
while low < high:
mid = (high + low + 1) // 2
comp = self.put(position, mid, m)
if comp > 0:
low = mid
else:
high = 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 fit_balls(self, positions, n, min_dist):
if n == 0:
return True
if len(positions) < 1:
return False
last_position = positions[0]
placement_count = 1
for p in positions:
if p - last_position >= min_dist:
last_position = p
placement_count += 1
return n <= placement_count
def maxDistance(self, position: List[int], m: int) -> int:
pos = sorted(position)
lo = 0
hi = pos[-1]-pos[0]
while lo < hi:
mid = hi - (hi-lo) // 2
can_fit = self.fit_balls(pos, m, mid)
if not can_fit:
hi = mid-1
else:
lo = mid
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()
l, r = 0, position[-1]
while l + 1 < r:
mid = (l + r) // 2
if self.is_ok(position, mid, m):
l = mid
else:
r = mid
if self.is_ok(position, r, m):
return r
return l
def is_ok(self, position, target, m):
count = 1
prev = position[0]
for i in range(1, len(position)):
diff = position[i] - prev
if diff >= target:
count += 1
prev = position[i]
return count >= 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:
position.sort()
def helper(dist):
count = 1
prev = position[0]
for i in range(1, len(position)):
if position[i] - prev >= dist:
prev = position[i]
count += 1
return count
low = 1
high = position[-1] - position[0]
res = float('-inf')
while low <= high:
mid = low + (high - low) // 2
if helper(mid) < m:
high = mid - 1
else:
res = max(res, mid)
low = 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()
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
# 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:
# Sort the array and evenly distribute the m balls
position.sort()
ans = top = (position[-1] - position[0]) // (m-1)
down = 0
while ans != down:
if self._test(position, ans, m-1): down = ans
else: top = ans
ans = (top + down) // 2
#print(ans, top, down)
return down
def _test(self, position, force, m):
i = 0
while i < len(position) and m > 0:
if len(position) - 1 - i < m:
return False
total = 0
while total < force:
i += 1
if i >= len(position): break
total += position[i] - position[i-1]
if total >= force: m -= 1
#print(i, total, force, m)
if m == 0: 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: List[int], m: int) -> int:
n = len(position)
def check(d):
i, b = 0, 1
for j in range(1, n):
if position[j]-position[i] >= d:
b += 1
i = j
return b >= m
position = sorted(position)
lo, hi = 1, position[-1]
while lo < hi:
mid = (lo+hi)//2
if check(mid):
lo = mid+1
else:
hi = mid
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 nnpossible(self, pos, k, m) -> bool:
prev = pos[0]
k -= 1
for i in pos:
if i - prev >= m:
k -= 1
prev = i
if k == 0:
break
return k == 0
def maxDistance(self, position: List[int], k: int) -> int:
position.sort()
l = 0
r = position[-1] + 1
while r - l > 1:
m = (l + r) // 2
if self.nnpossible(position, k, m):
l = m
else:
r = m
return l
# TTTFFF
|
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 numpy as np
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
# Choose m values from n such that the minimum of the differences is maximized
position.sort()
def check(check_val):
temp_m = m-1
previous_pos = position[0]
for p in position[1:]:
if p - previous_pos >= check_val:
previous_pos = p
temp_m -= 1
if temp_m == 0:
return True
return False
l = 0 # min return value
r = (position[-1] - position[0])//(m-1)+1 # max return value
ret = l
while l <= r:
val = (l+r)//2
if check(val):
ret = max(ret, val)
l = val + 1
else:
r = val-1
return ret
|
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)
def check(d):
i, b = 0, 1
for j in range(1, n):
if position[j]-position[i] >= d:
b += 1
i = j
if b >= m:
return True
return False
position = sorted(position)
lo, hi = 1, position[-1]
while lo < hi:
mid = (lo+hi)//2
if check(mid):
lo = mid+1
else:
hi = mid
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()
def possible(force):
prev = float('-inf')
balls_placed = 0
for x in position:
if x-prev >= force: # then that means we have that required force and we can keep a new ball here at position x
prev= x
balls_placed+=1
if balls_placed ==m:
return(True)
return(False)
lo,hi = 0, max(position)
while lo<hi:
mid = (lo+hi+1)//2
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:
def canFindGaps(d):
left = 0
found = i = 1
while i < len(position) and found < m:
if position[i] - position[left] >= d:
found += 1
left = i
i += 1
return found == m
position.sort()
max_diff = position[-1] - position[0]
if m == 2: return max_diff
left, right = 1, max_diff//(m-1)
while left < right:
mid = (left+right)//2
if canFindGaps(mid):
left = mid + 1
else:
right = mid - 1
if right < left:
return right
return left if canFindGaps(left) else 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, positions: List[int], m: int) -> int:
positions.sort()
def possible(positions,m,v):
cur=0
for i in range(1,len(positions)):
if positions[i]-positions[cur]>=v:
m-=1
cur=i
if m==1: return True
return m<=1
left,right=1,positions[-1]-positions[0]
ans=0
while left<=right:
v=(left+right)//2
if possible(positions,m,v):
ans=v
left=v+1
else:
right=v-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 isPossible(lst, m, force):
pos = lst[0]
for i in range(1, len(lst)):
if lst[i] - pos >= force:
m -= 1
pos = lst[i]
return m<2
l = 0
r = 1000000000
position.sort()
while l<r:
mid = ((l+r) >> 1) + 1
if isPossible(position,m,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:
'''
binary search
similar to 278. First Bad Version
'''
n = len(position)
position.sort()
def isfail(mid):
ans = 1
curr = position[0] # alike greedy idea, we just put the first ball at position 0!
for i in range(1, n):
if position[i] - curr >= mid:
ans += 1
curr = position[i]
return ans < m
res = float('-inf')
left = 1
right = position[-1] - position[0]
while left <= right:
mid = (left+right)//2
if isfail(mid):
right = mid-1
else:
left = mid + 1
res = max(res,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()
mx = position[-1]
lo, hi = 1, mx
def judge(x):
pre, cnt = -mx, 0
for p in position:
if p - pre >= x:
pre = p
cnt += 1
if cnt >= m:
return True
return False
while lo <= hi:
mid = (lo+hi)//2
if judge(mid):
lo = mid + 1
else:
hi = mid - 1
return hi
|
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
while l != r:
mid = (l+r)//2 + (l+r)%2
x = self.balls(position, mid)
if x < m:
r = mid-1
elif x >= m:
l = mid
return l
def balls(self, position, d):
ans = 1
cur = position[0]
for i in range(1, len(position)):
if position[i] - cur >= d:
cur = 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:
###weirdly not working :(
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:
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
# 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
# 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:
# 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, position: List[int], m: int) -> int:
if m==2:
return max(position)-min(position)
position.sort()
l,r=1,(max(position)-min(position)+1)//(m-1)
#print(l,r)
def helper(mindist):
#print(mindist)
cnt=m-1
cur=min(position)
for i in position[1:]:
if i-cur>=mindist:
cur=i
cnt-=1
if cnt==0:
return True
return False
if helper(r):
return r
while l<r:
mid=l+(r-l)//2
if helper(mid):
l=mid+1
else:
r=mid
#if helper(l):
#return l
#else:
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
|
def check(x,pos,m):
c=0
prev=-1000000000000000
for i in range(len(pos)):
if abs(pos[i]-prev)>=x:
c+=1
prev=pos[i]
return c>=m
class Solution:
def maxDistance(self, pos: List[int], m: int) -> int:
pos.sort()
n = len(pos)
#print(pos)
#pos = [-1000000000000000 pos + [10000000000000000]
l=1
r=pos[-1]
while l<=r:
mid = l + (r-l)//2
if check(mid,pos,m):
l=mid+1
else:
r=mid-1
if check(l,pos,m):
return l
else:
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()
delta = []
for i in range(len(position)-1):
delta.append(position[i+1]-position[i])
lo = min(delta)
hi = sum(delta)
m -= 1
def isPossible(minForce):
total = 0
count = 0
for d in delta:
total += d
if total >= minForce:
count += 1
total = 0
if count == m:
return True
return False
while lo <= hi:
mid = (lo + hi) // 2
if isPossible(mid):
lo = mid + 1
else:
hi = 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:
# #首先排序
# 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
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
result = 0
# can we place m balls each separated by x distance?
def check(x) -> bool:
nonlocal position, m
last = position[0]
placed = 1
for pos in position:
if pos - last >= x:
placed += 1
if placed >= m:
return True
last = pos
return False
first = 0
last = int(ceil((position[-1] - position[0])/(m-1)))
while first < last:
mid = (first + last + 1)//2
if check(mid):
result = mid
first = mid
else:
last = mid - 1
return first
|
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
|
# 1552. Magnetic Force Between Two Balls
# Binary search
def can_distribute (baskets, ball_count, min_dist):
last = []
for basket in baskets:
if not last or last[-1] + min_dist <= basket:
last.append (basket)
if len (last) == ball_count:
return True
return False
def first (a, b, p):
if a >= b:
return a
elif p(a):
return a
elif not p(b-1):
return b
else:
m = (a + b) // 2
if p(m-1):
return first (a, m, p)
elif not p(m):
return first (m, b, p)
else:
return m
def last (a, b, p):
return first (a, b, lambda x: not p(x)) - 1
def max_dist (baskets, ball_count):
baskets = sorted (baskets)
theoretical_max_dist = (baskets[-1] - baskets[0]) // (ball_count - 1)
return last (1, theoretical_max_dist + 1, lambda dist: can_distribute (baskets, ball_count, dist))
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
return max_dist(position, 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:
position = sorted(position)
res = 0
def check(n):
count = 0
cur = -1
for i in range(len(position)):
if cur < 0:
cur = position[i]
continue
if position[i]- cur >= n:
cur = position[i]
count+=1
return count+1
l, r = 0, position[-1]- position[0]
while l <= r:
mid = (l+r)//2
if check(mid) >= m:
res = max(mid, res)
l= mid+1
else:
r = 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()
def getCount(d: int) -> int:
last, count = position[0], 1
for x in position:
if x - last >= d:
last = x
count += 1
return count
l, r = 0, position[-1] - position[0] + 1
t = r
while l < r:
mid = l + (r - l) // 2
if getCount(t - mid) >= m:
r = mid
else:
l = mid + 1
return t - 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:
pos = sorted(position)
l = 0
r = pos[-1] - pos[0]
while l < r:
mid = (l + r + 1) // 2
cum = pos[0]
putted = 1
for p in pos:
if p >= cum + mid:
cum = p
putted += 1
if putted >= 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 = sorted(position)
high = position[-1] - position[0]
if m == 2:
return high
low = high
for i in range(1, len(position)):
low = min(position[i] - position[i - 1], low)
if m == len(position):
return 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:
position.sort()
l, r = 1, position[-1] - position[0]
while l < r:
mid = (l + r) // 2
if self.check(position, mid, m):
l = mid + 1
else:
r = mid
return l if self.check(position, l, m) else l-1
def check(self, position, k, m):
last_placed_position = None
for i,p in enumerate(position):
if i == 0 or p-last_placed_position >= k:
m -= 1
if m == 0:
return True
last_placed_position = p
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 is_valid(self, arr, force, m):
cnt = 1
prev = arr[0]
for i in range(1, len(arr)):
if arr[i] - prev >= force:
cnt += 1
prev = arr[i]
if cnt == m:
return True
return False
def maxDistance(self, position: List[int], m: int) -> int:
res = -1
position.sort()
left = 1
right = position[len(position) - 1]
while left + 1 < right:
mid = (right - left) // 2 + left
if (self.is_valid(position, mid, m)):
# Too small
left = mid
res = max(res, mid)
else:
# Too big
right = mid
for i in range(left, right + 1):
if self.is_valid(position, i, m):
res = max(res, i)
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 small_enough(mid):
count = prev = 0
for i, p in enumerate(position):
if i == 0:
count += 1
elif p - prev >= mid:
count += 1
else:
continue
prev = p
return count >= m
l, h = 1, position[-1] - position[0]
while l < h:
mid = (l + h + 1) // 2
if small_enough(mid):
l = mid
else:
h = 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:
prev= x
balls_placed+=1
return(balls_placed>=m)
lo,hi = 0, max(position)
while lo<hi:
mid = (lo+hi+1)//2
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()
posSorted = position
low = 1
high = posSorted[-1]
mid = (low+high)//2
while low < high:
if self.validForce(posSorted, m, mid):
low = mid
else:
high = mid-1
mid = high-(high-low)//2
return low
def validForce(self, positions, m, mid):
used = 0
lastpos = 0
for i, pos in enumerate(positions):
if i == 0:
used += 1
continue
if (pos - positions[lastpos]) >= mid:
lastpos = i
used += 1
if used >= m:
return True
return False
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.