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, A: List[int], m: int) -> int:
A, n = sorted(A), len(A)
left, right = 0, A[-1] - A[0]
while left < right:
mid = (right + left + 1) // 2
ct, idx = 1, 0
for i in range(1, n):
if A[i] - A[idx] >= mid:
ct += 1
idx = i
if ct < m:
right = mid - 1
else:
left = mid
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 num_balls(min_dist: int) -> int:
ans, curr = 1, 0
for i in range(1,n):
if position[i] - position[curr] >= min_dist:
ans += 1
curr = i
return ans
l, r = 0, position[n-1] - position[0]
while l < r:
mid = r - (r - l) // 2
if num_balls(mid) >= m:
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
lo,hi = 1,(position[-1]-position[0])//(m-1) + 1
res = 0
def possible(gap):
balls = m
prev = None
for i in range(len(position)):
if not prev:
prev = position[i]
balls-=1
else:
if abs(prev - position[i])<gap:
continue
else:
prev = position[i]
balls-=1
if balls<=0:
return True
return False
while lo<=hi:
# print(lo,hi)
mid = (lo+hi)//2
if possible(mid):
res = mid
lo = mid+1
else:
hi = 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, positions: List[int], m: int) -> int:
positions.sort()
def possible(force):
placed = 0
pos = -math.inf
for p in positions:
if p - pos >= force:
placed += 1
pos = p
return placed >= m
(lo, hi) = (0, positions[-1])
while lo <= hi:
mid = (lo + hi) // 2
if possible(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
|
def pos(mid, position, m):
res = 1
curr_pos = 0
for i in range(1, len(position)):
if position[i] - position[curr_pos] >= mid:
curr_pos = i
res += 1
if res == m:
return True
return False
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
l = 0
h = position[-1] - position[0]
res = 0
while l <= h:
mid = int((h + l) / 2)
if pos(mid, position, m) == False:
h = mid - 1
else:
res = max(res, mid)
l = mid + 1
return res
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def _MaxPossibleForce(self, position):
return position[-1] - position[0]
def _AssignBallsWithKDistance(self, position, force):
i = 1
assigned = 1
# 0th item is the first ball
last_assigned_position = 0
while i < len(position):
if position[i] - position[last_assigned_position] >= force:
assigned += 1
last_assigned_position = i
i += 1
return assigned
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
max_force = self._MaxPossibleForce(position)
min_force = 1
print((self._AssignBallsWithKDistance(position, 4)))
while min_force <= max_force:
search_force = (min_force + max_force)//2
num_assigned = self._AssignBallsWithKDistance(position, search_force)
if num_assigned < m:
max_force = search_force - 1
else:
min_force = search_force + 1
return max_force
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
#https://leetcode.com/problems/magnetic-force-between-two-balls/discuss/854038/Beginner-Friendly-solution-or-Explained-with-time-complexity-analysis
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def check(dist):
prev = position[0]
cnt = 1
idx = 0
while idx<len(position):
curr = position[idx]
if curr-prev>=dist:
cnt += 1
prev = curr
idx += 1
if cnt>m:
break
return cnt
position.sort()
L = 1
R = position[-1]-position[0]
while L<R:
# print ('a ', L, R)
if L==R-1:
if check(R)>=m:
return R
else:
return L
mid = L+(R-L)//2
if check(mid)<m:
R = mid-1
else:
L = mid
# print (L, R)
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 solution.
# https://leetcode.com/problems/magnetic-force-between-two-balls/discuss/794070/Python-Binary-search-solution-with-explanation-and-similar-questions
position.sort()
def num_balls_placed(tested_distance):
num_placed, current = 1, position[0]
for i in range(1, len(position)):
if position[i] - current >= tested_distance:
current = position[i]
num_placed += 1
return num_placed
low, high = 0, position[-1] - position[0]
best = -1
while low <= high:
mid = (low + high) // 2
if num_balls_placed(mid) < m:
high = mid - 1
else:
best = mid
low = mid + 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()
maxrange = position[-1] - position[0]
# print(maxrange)
l, r = 1, maxrange
while l <= r:
mid = l + (r - l) // 2
cnt = self.count(position, mid)
if cnt >= m:
l = mid + 1
else:
r = mid - 1
return r
def count(self, poss: list, dist: int) -> int:
res = 1
last = poss[0]
for i in range(1, len(poss)):
if poss[i] - last >= dist:
res += 1
last = poss[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 allow(self, mid, position, m):
#check if the given distance is possible
#greedy approach: putting each ball in the first place
balls = 1
last = position[0]
for i in range(1, len(position)):
if(position[i] - last >= mid):
balls+=1
last = position[i]
return balls>=m
def maxDistance(self, position: List[int], m: int) -> int:
o = Solution()
#binary search
position.sort()
low = 0
high = 1000000000
pos = 0
while(low<=high):
mid = int((high+low)/2)
if(o.allow(mid, position, m)):
low = mid+1
pos = mid
else:
high = mid-1
return pos
|
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, p: List[int], m: int) -> int:
def isFeasible(mid, arr, n, k):
pos = arr[0]
elements = 1
for i in range(1, n):
if (arr[i] - pos >= mid):
pos = arr[i]
elements += 1
if (elements == k):
return True
return False
p.sort()
#[1,2,3,4,5,100000]
#Cm(n)
n = len(p)
res = -1
left = 0
right = p[n - 1]
while left < right:
mid = (left + right) // 2
if (isFeasible(mid, p, n, m)):
res = max(res, mid)
left = mid + 1
else:
right = mid
return res
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
position.sort()
def count(d):
answer, curr = 1, position[0]
for i in range(1,n):
if position[i] - curr >= d:
answer+=1
curr = position[i]
return answer
left, right = 0, position[-1] - position[0]
while left < right :
mid = right - (right- left)//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 = sorted(position)
# def enough(distance):
# cur = position[0]
# c = 1
# for i in range(len(position)):
# if position[i] >= cur + distance:
# cur = position[i]
# c += 1
# return c>=m
# cur_max = position[-1]-position[0] + 1
# cur_min = 1
# while abs(cur_min-cur_max)>1:
# mid = cur_min + (cur_max-cur_min)//2
# if enough(mid):
# cur_min = mid
# else:
# cur_max = mid
# return cur_min
position = sorted(position)
n = len(position)
if n == 2:
return position[-1]-position[0]
cur_min = 1
cur_max = position[-1]-position[0]+1
def enough(distance):
ini = position[0]
c = 1
for i in range(1, n):
if position[i]-ini >= distance:
ini = position[i]
c += 1
return c>=m
while(abs(cur_max-cur_min)>1):
mid = (cur_max+cur_min)//2
if enough(mid):
cur_min = mid
else:
cur_max = mid
return cur_min
|
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 isPossible(self, position , m , force):
lastKept = position[0]
m -= 1
for i in range(1,len(position)):
currentPosition = position[i]
if currentPosition - lastKept >= force:
lastKept = currentPosition
m -= 1
return m <= 0
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
left , right = 1, 10 ** 9 + 1
result = -1
while left <= right:
mid = (left + right) // 2
if self.isPossible(position,m,mid):
result = mid
left = mid + 1
else:
right = mid - 1
return result
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def distribute(self, position, m, dist):
prev = position[0]
m -= 1
for i in range(1, len(position)):
if position[i]-prev >= dist:
prev = position[i]
m -= 1
if m==0:
return True
return False
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
low, high = 0, position[-1]-position[0]
res = 0
while low <= high:
mid = (high-low)//2 + low
if self.distribute(position, m, mid):
res = mid
low = mid + 1
else:
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()
lo, hi = 0, position[-1] - position[0]
def possible(target, m):
idx = prev = 0
while m:
if idx >= len(position):
return False
if not prev or position[idx] >= prev + target:
m -= 1
prev = position[idx]
else:
idx += 1
return True
while lo < hi:
mid = (lo + hi + 1) // 2
if possible(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()
def count(d):
c = position[0]
res = 1
for n in position[1:]:
if n-c>=d:
res += 1
c = n
return res
l = 1
r = (position[-1] - position[0])//(m-1)
while l <= r:
mid = (l+r)//2
if count(mid) < m:
r = mid - 1
elif count(mid+1)<m:
return mid
else:
l = mid +1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
arr = sorted(position)
if m == 2:
return arr[-1] - arr[0]
def is_possible(f):
count = 1
origin = arr[0]
for i in range(1, n):
if arr[i] - origin >= f:
count += 1
origin = arr[i]
if count >= m:
return True
if count < m - 1 or arr[-1] - origin < f:
return False
return True
low, high = 1, arr[-1] - arr[0]
while low + 1 < high:
f = ceil((high + low) / 2)
if is_possible(f):
low = f
else:
high = f
if is_possible(high):
return high
return low
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def check(f):
nonlocal position
nonlocal m
nonlocal res
for i in range(len(position)):
balls_cnt = 1
last_ball_pos = 0
for i in range(1, len(position)):
last_ball_val_pos = position[last_ball_pos]
curr_ball_val_pos = position[i]
cur_force = curr_ball_val_pos - last_ball_val_pos
if cur_force >= f:
balls_cnt +=1
last_ball_pos = i
if balls_cnt == m:
res = max(res, f)
return True
return False
res = 0
position.sort()
left = 1
right = max(position)
while left < right:
mid = left + (right - left) // 2
if check(mid):
# add to answer, check if can do better increase mid and look up in right part
left = mid + 1
else:
right = mid
return res
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def check(position,m,f):
prev=0
balls=1
index=1
while index<len(position) and balls<=m:
if position[index]-position[prev]>=f:
prev=index
index+=1
balls+=1
else:
index+=1
if balls>=m:
return True
else:
return False
position=sorted(position)
start=1
end=max(position)
while start<end:
mid=(start+end)//2
if check(position,m,mid):
start=mid+1
else:
end=mid
return start-1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def feasible(self, d, m):
last_pos = self.position[0]
i = 1
num_placed = 1
while num_placed < m and i < self.plen:
if self.position[i] - last_pos >= d:
last_pos = self.position[i]
num_placed += 1
i += 1
return num_placed == m
def maxDistance(self, position: List[int], m: int) -> int:
self.position = position
self.plen = len(self.position)
self.position.sort()
left = 1
right = self.position[-1] - self.position[0] + 1
while left + 1 < right:
mid = (left + right) // 2
if self.feasible(mid, m):
left = mid
else:
right = 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, pos: List[int], m: int) -> int:
pos.sort()
n = len(pos)
if m == 2: return pos[-1] - pos[0]
def valid(k):
count, cur = 1, pos[0]
for p in pos[1:]:
if p - cur >= k:
count += 1
cur = p
return count >= m
l, r = 0, pos[-1] - pos[0]
ans = 0
while l < r:
mid = (l + r) // 2
if valid(mid):
ans = max(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:
position = sorted(position)
def isvalid(gap, m):
j = 0
for i in range(len(position)):
if(position[i]-position[j]>=gap):
m -= 1
j = i
if m == 0:
return True
return False
low, high = 1, position[-1]
sem = 0
while(low<=high):
mid = (high + low)//2
if isvalid(mid, m-1):
sem = mid
low = mid + 1
else:
high = mid - 1
return sem
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, pos: List[int], m: int) -> int:
pos.sort()
def isfeasible(dist):
cur = pos[0]
n = 1
for i in range(len(pos)):
if pos[i] >= cur + dist:
cur = pos[i]
n += 1
return n>=m
ng = pos[-1] - pos[0] + 1
ok = 1
while abs(ok-ng)>1:
mid = ok + (ng-ok)//2
if isfeasible(mid):
ok = mid
else:
ng = mid
return ok
|
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()
## need to find the maximum minmum distance
## feasibility is a function of distance
## need to find the last feasible solution
## that is the last T
def check (dist: int) -> bool:
curr = position[0]
count = 1
for i in range (len(position)):
#print(abs(position[i]-curr))
if (abs(position[i]-curr)>= dist):
curr = position[i]
count +=1
if(i == (len(position)-1)):
if (count >= m):
return True
else:
return False
## BS for optimal length
lo = 1
hi = (max(position)- min(position))
while (lo<hi):
mid= lo + ((hi-lo+1)//2)
#print(mid)
if(check(mid)):
lo = mid
else :
hi = mid -1
return (lo)
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
position.sort()
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
|
def isPossible(arr, n, C, mid):
magnet = 1
currPosition = arr[0]
for i in range(1, n):
if (arr[i] - currPosition >= mid):
magnet += 1
currPosition = arr[i]
if (magnet == C):
return True
return False
def binarySearch(n, C, arr):
arr.sort(reverse = False)
lo = 0
hi = arr[n - 1]
ans = 0
while (lo <= hi):
mid = int((lo + hi) / 2)
if (isPossible(arr, n, C, mid) == False):
hi = mid - 1
else:
ans = max(ans, mid)
lo = mid + 1
return ans
class Solution:
def maxDistance(self, arr: List[int], k: int) -> int:
n = len(arr)
return binarySearch(n, k, arr)
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, pos: List[int], m: int) -> int:
pos.sort()
n = len(pos)
l = 1
r = 10 ** 9
# def isOK(gap):
# i = 0
# ii = 0
# nn = 1
# while i < n:
# if pos[i] - pos[ii] >= gap:
# nn += 1
# ii = i
# i += 1
# return nn >= m
# while l < r:
# mid = (l + r + 1) // 2
# if isOK(mid):
# l = mid
# else:
# r = mid - 1
# return l
def isNotOK(gap):
i = 0
ii = 0
nn = 1
while i < n:
if pos[i] - pos[ii] >= gap:
nn += 1
ii = i
i += 1
return nn < m
# find the smallest unvalid solve then minus 1
while l < r:
mid = (l + r) // 2
if isNotOK(mid):
r = mid
else:
l = mid + 1
return l - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
## need to find the maximum minmum distance
## feasibility is a function of distance
## need to find the last feasible solution
## that is the last T
def check (dist: int) -> bool:
curr = position[0]
count = 1
for i in range (len(position)):
#print(abs(position[i]-curr))
if (abs(position[i]-curr)>= dist):
curr = position[i]
count +=1
if(i == (len(position)-1)):
if (count >= m):
return True
else:
return False
## BS for optimal length
lo = 1
hi = (max(position)- min(position))
while (lo<hi):
mid= lo + ((hi-lo+1)//2)
#print(mid)
if(check(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:
l, r, res = 0, max(position), 0
position.sort()
while l < r:
mid = l + (r - l) // 2
i, cnt = 0, 1
while i < len(position):
j = i + 1
while j < len(position):
if position[j] - position[i] >= mid:
cnt += 1
break
j += 1
i = j
if cnt == m:
break
if cnt == m:
l = mid + 1
res = max(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 , right = 0, position[-1] - position[0]
while left <= right:
mid = (left + right) // 2
count = 1
curr = position[0]
for x in position[1:]:
if x - curr >= mid:
count += 1
curr = x
if count >= 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
|
from math import floor
class Solution:
def is_min_dis_possible(self, positions, m, min_dis):
prev = 0
cur = 0
while m > 0:
m -= 1
while cur < len(positions) and positions[cur] - positions[prev] < min_dis:
cur += 1
if cur >= len(positions):
break
prev = cur
return True if m == 0 else False
def maxDistance(self, position, m):
position_sorted = sorted(position)
high = position_sorted[-1]
low = 0
while low <= high:
mid = floor((high + low) / 2)
if mid == low:
break
is_min_dis_possible = self.is_min_dis_possible(position_sorted, m, mid)
if is_min_dis_possible:
low = mid
else:
high = mid
return low
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def possible(d):
cnt = 1
cur = position[0]
for i in range(1, len(position)):
if position[i] - cur >= d:
cnt += 1
cur = position[i]
if cnt >= m:
return True
return False
lo = 0
hi = position[-1] - position[0]
while lo < hi:
mid = hi - (hi - lo) // 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:
n = len(position)
position.sort()
def valid(dis):
prev = 0
balls = 1
for i in range(1, n):
if position[i] - position[prev] >= dis:
balls += 1
prev = i
return balls >= m
lo, hi = 0, 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, position: List[int], m: int) -> int:
# 二分查找
# 判断若x为最小磁力,能否分隔出m-1个间隔
def Valid(x):
ans = 0
target = position[0]
for i in range(1,len(position)):
if position[i] - target >= x:
ans += 1
target = position[i]
return ans >= m - 1
position.sort()
l,r = min(position[i + 1] - position[i] for i in range(len(position) - 1)),(position[-1] - position[0]) // (m - 1)
while l <= r:
mid = l + (r - l) // 2
# 若最小磁力可以分成m-1个间隔,则可能存在更大值,否则最小磁力值应当更小
if Valid(mid):
l = mid+1
else:
r = mid - 1
return l-1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
lo, hi = 0, position[-1] - position[0]
def possible(target, m):
idx = prev = 0
while m:
if idx >= len(position):
return False
if not prev or position[idx] >= prev + target:
m -= 1
prev = position[idx]
else:
idx += 1
return True
while lo <= hi:
mid = (lo + hi) >> 1
if possible(mid, m):
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:
def feasible(min_req_dist, b):
i = 0
prev_pos = None
while i < len(position) and b > 0:
if prev_pos == None or position[i] - prev_pos >= min_req_dist:
b -= 1
prev_pos = position[i]
i += 1
return b == 0
position.sort()
n = len(position)
low = 1
high = position[-1]
while low < high:
mid = (low + high) // 2 + 1
if feasible(mid, m):
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 maxDistance(self, position: List[int], m: int) -> int:
position.sort()
start, end = 0, position[-1]
while start + 1 < end:
mid = (start + end) // 2
if self.isValid(position, mid) < m:
end = mid
else:
start = mid
if self.isValid(position, end) < m:
return start
else:
return end
def isValid(self, position, dist):
cnt = 1
i = 1
current = position[0] + dist
while i < len(position):
if position[i] >= current:
cnt += 1
current = position[i] + dist
i += 1
return cnt
|
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 feasible(d, position):
# print(\"Check d: \", d)
balls = [position[0]]
placed = 1
i = 1
while i <= len(position) - 1:
if position[i] - balls[-1] >= d:
balls.append(position[i])
# print(balls)
placed += 1
if placed == m:
return True
i += 1
return False
position.sort()
left, right = 1, max(position)
while left < right:
mid = right - (right - left) // 2
if feasible(mid, position):
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 isSolution(self, m, position, minforce):
index = 0
remaining = m
for bin in position:
if bin >= index:
remaining-=1
index = bin + minforce
if remaining == 0:
return True
return False
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
maxDist = position[-1]
minDist = 0
while maxDist > minDist + 1:
average = (minDist + maxDist) // 2
#print (\"average \", average)
if Solution.isSolution (self, m, position, average):
#print (\"is Sol\")
minDist = average
else:
maxDist = average
#print (\"not Sol\")
if Solution.isSolution (self, m, position, maxDist):
return maxDist
else:
return minDist
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, pos: List[int], m: int) -> int:
pos.sort()
n = len(pos)
# def check(gap):
# i = 0
# ii = 0
# nn = 1
# while i < n:
# if pos[i] - pos[ii] >= gap:
# nn += 1
# ii = i
# i += 1
# return nn >= m
# l = 1
# r = 10 ** 9
# while l < r:
# mid = (l + r + 1) // 2
# if check(mid):
# l = mid
# else:
# r = mid - 1
# return l
def isNotOK(gap):
i = 0
ii = 0
nn = 1
while i < n:
if pos[i] - pos[ii] >= gap:
nn += 1
ii = i
i += 1
return nn < m
l = 1
r = 10 ** 9
while l < r:
mid = (l + r) // 2
if isNotOK(mid):
r = mid
else:
l = mid + 1
return l - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def check(min_force):
count = 1
k = position[0]
for n in position:
if n-k >= min_force:
count += 1
k = n
if count >= m:
return True
return False
i, j = 1, position[-1]-position[0]
while i <= j:
mid = (i+j)//2
c1 = check(mid)
c2 = check(mid+1)
if c1 and not c2:
return mid
if not c1:
j = mid-1
else:
i = mid+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()
l, r = 0, position[-1] - position[0] + 1
def c(x):
y = m
y -= 1
last = 0
j = 0
for _ in range(y):
while j < len(position) and position[j] - position[last] < x:
j += 1
if j == len(position):
# print(j, last, x)
return 0
last = j
return 1
while l < r:
mid = (r+l)//2
dist = c(mid)
# print('len', mid, l, r, dist)
if not dist:
r = mid
else:
l = mid+1
return l-1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def f(mid,position,m):
f=position[0]
m=m-1
for j in range(1,len(position)):
if position[j]-f>=mid:
f=position[j]
m=m-1
if m<0:
break
if m<=0:
return True
return False
h=(position[-1]-position[0])//(m-1)+1
l=0
while h>=l:
mid=(h+l)//2
print(mid)
if f(mid,position,m):
l=mid+1
else:
h=mid-1
return l-1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, 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:
position.sort()
high=position[-1]-position[0]
low = high
for i in range(1, len(position)):
low = min(low, position[i]-position[i-1])
def feasible(force):
last = position[0]
count=1
for i in range(1,len(position)):
if (position[i]-last)>=force:
last=position[i]
count+=1
if count >= m:
return True
return False
while low < high:
mid = (low+high)//2
if not feasible(mid):
high=mid
else:
low=mid+1
low-=1
ans=float('inf')
last=position[0]
for i in range(1, len(position)):
if (position[i]-last)>=low:
ans=min(ans, position[i]-last)
last=position[i]
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:
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
print(l)
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 valid_distance(d):
cur, idx, total = 0, 1, 1
while idx < len(position) and total < m:
if position[idx] - position[cur] >= d:
total += 1
cur = idx
idx += 1
return total == m
position.sort()
s, e= 0, position[-1] - position[0] + 1
while s < e:
mid = s + (e - s) // 2
if valid_distance(mid):
s = mid + 1
else:
e = mid
return s - 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()
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
right = max(position) - min(position) + 1
while left < right:
mid = (left+right)//2
if isfail(mid):
right = mid
else:
left = mid + 1
return left - 1 # left is the min value to fail, so left-1 is the max value to succeed!
# 因为求的是最大值!所以需要巧妙使用binary search!
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position = sorted(position)
lo = 1
hi = position[-1]-position[0]
while lo<hi:
mid = (lo+hi+1)//2
# count number of balls that can be placed if distance at least mid
ind1 = 0
ind2 = 1
count = 1
while ind2<len(position) and count<m:
if position[ind2]-position[ind1]>=mid:
count += 1
ind1 = ind2
ind2 += 1
if count>=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
|
from functools import lru_cache
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
if m == 2:
return position[-1] - position[0]
def can_place(mid): # mid is candidate for minimum magnetic force
num_placed = 1
prev = position[0]
for num in position[1:]:
if num - prev >= mid:
num_placed += 1
prev = num
return num_placed >= m
left = 0
right = position[-1] - position[0]
res = 0
while left < right:
mid = (left + right) // 2
if can_place(mid):
left = mid + 1
else:
right = mid
return left - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def feasible(min_req_dist, b):
i = 0
prev_pos = None
while i < len(position) and b > 0:
if prev_pos == None or position[i] - prev_pos >= min_req_dist:
b -= 1
prev_pos = position[i]
i += 1
return b == 0
# This is a fantastic example of establishing a monotonic predicate and
# squeeze from the right side of the search space (instead of the usual left-side)
position.sort()
low = 1
high = position[-1]
while low < high:
# Since we constantly trying to see if we could find a feasible solution that's
# greater than the current one, we want to always make progress by advancing
# mid to the right. (low + high) // 2 + 1 guarantees this, even when we have
# just two elements. mid will be pointing at high. Otherwise we'd get an infinite
# loop with (low + high) // 2
mid = (low + high) // 2 + 1
if feasible(mid, m):
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 maxDistance(self, position: List[int], m: int) -> int:
def distributable(n):
pos = len(position) - 1
balls = m
while pos >= 0 and balls:
balls -= 1
np = position[pos] - n
while pos >= 0 and position[pos] > np:
pos -= 1
return not balls
position = sorted(position)
lo, hi = 1, (position[-1] - position[0]) // (m -1) + 1
ans = lo
while lo < hi:
mid = lo + (hi - lo) // 2
if distributable(mid):
ans = mid
lo = mid + 1
else:
hi = mid
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def trial(n):
prev, curr, ct = 0, 1, 1
while True:
if curr >= len(position):
return False
if position[curr] - position[prev] >= n:
prev = curr
ct += 1
if ct == m:
return True
curr += 1
l, r = 1, position[-1] - position[0] + 1
ret = 0
while l < r:
mid = (l + r) // 2
ans = trial(mid)
if ans:
l = mid + 1
else:
r = mid
return r - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def ispossible(f: int) -> bool:
Count, pos = 1, 0
for i in range(1, len(position)):
if position[i] - position[pos] >= f:
pos = i
Count += 1
return bool(Count >= m)
start, end = 1, position[-1] - position[0]
while start < end:
mid = (start + end)//2
if ispossible(mid) and not ispossible(mid + 1): return mid
elif ispossible(mid): start = mid + 1
else: end = mid - 1
return start
|
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()
@lru_cache(None)
def helper(gap):
prev = 0
cnt = 1
while cnt < m:
idx = prev + 1
while idx < len(position) and position[idx] - position[prev] < gap:
idx += 1
if idx >= len(position): break
prev = idx
cnt += 1
return cnt == m
lb, ub = 1, position[-1] - position[0] + 1
while lb < ub:
mid = lb + (ub - lb)//2
if helper(mid):
if not helper(mid + 1): return mid
else: lb = mid + 1
else: ub = mid
return lb
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
high = position[-1] - position[0]
low = 1
while high != low:
mid = (high + low + 1) // 2
balls = 1
start = position[0]
for i in range(1, n):
if position[i] - start >= mid:
balls += 1
start = position[i]
if balls >= m:
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 maxDistance(self, position: List[int], m: int) -> int:
position.sort()
left, right = 1, (position[-1]-position[0])//(m-1) + 2
while left < right:
mid = left + (right-left)//2 + 1
if self.can_use_as_min(position, mid, m):
left = mid
else:
right = mid-1
return left
def can_use_as_min(self, position, min_distance, m):
m -= 1
i = 1
last_position = position[0]
while m > 0:
while position[i] - last_position < min_distance:
i += 1
if i >= len(position):
return False
last_position = position[i]
m -= 1
return True
|
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)
pos = sorted(position)
if m == 2:
return pos[-1] - pos[0]
def isFeasible(d):
j = 0
for k in range(1, m):
j = bisect.bisect_left(pos, pos[j] + d, j + 1)
if j == n:
return False
return True
res = -1
lo, hi = 1, pos[-1] - pos[0] + 1
while lo < hi:
d = (lo + hi) // 2
print(lo, hi, d)
if isFeasible(d):
lo = d + 1
else:
hi = d
return hi - 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()
l, r, sol = 0, position[-1] - position[0], -1
def helper(target):
pre, cnt = position[0], 1
for n in position:
temp = n - pre
if temp >= target:
cnt += 1
pre = n
if (position[-1] - pre) >= target: cnt += 1
if cnt >= m: return True
return False
while l <= r:
mid = (l + r) // 2
if (helper(mid)):
l = mid + 1
else:
r = mid - 1
return l - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
L = len(position)
left = 1
right = position[-1]-position[0]
def can_place(f):
n_ball = m-1
cum_dis = 0
p = 1
#n_ball -= 1
while n_ball >0 and p<L :
cum_dis += position[p]-position[p-1]
if cum_dis >= f:
n_ball -= 1
cum_dis = 0
p += 1
if n_ball == 0:
return True
else:
return False
#return can_place(4)
# 1, 9, 5 false
# 1, 4, 2, true
# 2, 4, 3, true
# 3, 4, 3,
while left < right:
mid = (left+right)//2
if can_place(mid):
left = mid
else:
right = mid-1
if right-left == 1:
if can_place(right):
return right
else:
return left
#return can_place(3)
return left
# if m == 2:
# return position[-1] - position[0]
# L = len(position)
# hp = []
# f_set = {}
# b_set = {}
# for i in range(L-1):
# heapq.heappush(hp, (position[i+1]-position[i], position[i], position[i+1]))
# f_set[position[i]] = position[i+1]
# b_set[position[i+1]] = position[i]
# total = L-1
# while total > m-1:
# l, x, y = heapq.heappop(hp)
# if x in f_set and y in b_set and f_set[x] == y and b_set[y] == x:
# left_len = float(inf)
# right_len = float(inf)
# if y in f_set:
# right_len = f_set[y]-y
# if x in b_set:
# left_len = x-b_set[x]
# if left_len < right_len:
# #merge with left
# new_y = y
# new_x = b_set[x]
# del f_set[x]
# del b_set[x]
# f_set[new_x] = new_y
# b_set[new_y] = new_x
# ret = new_y - new_x
# else:
# # merge with right
# new_y = f_set[y]
# new_x = x
# del f_set[y]
# del b_set[y]
# f_set[new_x] = new_y
# b_set[new_y] = new_x
# ret = new_y - new_x
# heapq.heappush(hp, (new_y-new_x, new_x, new_y))
# total -= 1
# ret = float(inf)
# for k in f_set.keys():
# ret = min(ret, f_set[k]-k)
# return f_set
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, A, m: int) -> int:
A.sort()
def test(mid):
cnt = m - 1
cur = A[0]
for i in A[1:]:
if i - cur >= mid:
cnt -= 1
cur = i
else:
continue
if cnt == 0: return True
return False
mi = 1
ma = (A[-1] - A[0]) // (m - 1)
while mi <= ma:
mid = (mi + ma) // 2
if test(mid):
mi = mid + 1
else:
ma = mid - 1
return ma
|
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)
minp, maxp = position[0], position[-1]
lo, hi = 1, (maxp - minp) // (m -1) + 1
ans = lo
def distributable(n):
pos = position.copy()
balls = m
while pos and balls:
balls -= 1
np = pos[-1] - n
while pos and pos[-1] > np:
pos.pop()
return not balls
while lo < hi:
mid = lo + (hi - lo) // 2
if distributable(mid):
ans = mid
lo = mid + 1
else:
hi = mid
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], cuts: int) -> int:
position = sorted(position)
l, r = 1, position[-1] - position[0]
while l != r:
m = (l + r) // 2 + 1
if self.isValid(position, m, cuts):
l = m
else:
r = m - 1
return l
def isValid(self, position, force, cuts):
c = 0
l = 0
for r in range(1, len(position)):
if position[r] - position[l] >= force:
c += 1
l = r
return c >= cuts-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, p: List[int], m: int) -> int:
n=len(p)
p.sort()
def check(x):
st=p[0]
cnt=1
for i in range(1,n):
if abs(p[i]-st)>=x:
st=p[i]
cnt+=1
return cnt>=m
lo=1
hi=max(p)+4
print(check(999999999))
while lo<=hi:
mi=(lo+hi)//2
if check(mi):
ans=mi
lo=mi+1
else:
hi=mi-1
#print(mi,check(mi))
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:
# binary search
def isValid(f, k):
# return true if it is possible to arrange m elements in array such that minimum distance is f
pos = position[0]
count = 1
for i in range(1, n):
if (position[i] - pos >= f):
# put it here
pos = position[i]
count += 1
if count == k:
return True
return False
n = len(position)
position.sort()
res = 0
l, r = 0, position[n-1] - position[0] + 1
while l < r:
mid = (l+r) >> 1
if isValid(mid, m):
res = max(res, mid)
l = mid + 1
else:
r = mid
return res
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
## need to find the maximum minmum distance
## feasibility is a function of distance
## need to find the last feasible solution
## that is the last T
def check (dist: int) -> bool:
curr = position[0]
count = 1
for i in range (len(position)):
#print(abs(position[i]-curr))
if (abs(position[i]-curr)>= dist):
curr = position[i]
count +=1
if (count>=m):
return True
if(i == (len(position)-1)):
if (count >= m):
return True
else:
return False
## BS for optimal length
lo = 1
hi = (max(position)- min(position))
while (lo<hi):
mid= lo + ((hi-lo+1)//2)
#print(mid)
if(check(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 possible(mid):
temp = m-1
prev = position[0]
for i in range(len(position)):
if(position[i] - prev>=mid):
prev = position[i]
temp-=1
return(temp<=0)
l,r = 0,max(position)
position.sort()
while(l<r):
mid = (l+r)//2
print((l,mid,r))
if(r == l+1):
if(possible(r)):
l = r
else:
r = l
break
if(possible(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, arr: List[int], m: int) -> int:
arr.sort()
def isFeasible(mid):
pos = arr[0]
# Initialize count of elements placed.
elements = 1
# Try placing k elements with minimum
# distance mid.
for i in range(1, len(arr)):
if (arr[i] - pos >= mid):
# Place next element if its distance
# from the previously placed element
# is greater than current mid
pos = arr[i]
elements += 1
# Return if all elements are placed
# successfully
if (elements == m):
return True
return 0
hi = arr[-1] - arr[0]
lo = float('inf')
for i in range(len(arr)-1):
lo = min(lo, arr[i+1]-arr[i])
ans = float('-inf')
while lo<=hi:
mid = (lo+hi)//2
if isFeasible(mid):
ans = max(ans, mid)
lo = mid+1
else:
hi = mid-1
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
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 maxNumberBallsCanBePlaced(minDis):
count = 1
end = position[0]
for i in range(len(position)):
if(position[i] - end >= minDis):
end = position[i]
count += 1
return count
position.sort()
left = 1
right = position[-1]
while(left < right):
mid = left + (right - left) // 2
if(maxNumberBallsCanBePlaced(mid) >= m):
left = mid + 1
else:
right = mid
return left - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
size = len(position)
# --------------------------------------------
def satisfy( gap ) -> bool:
'''
input: gap
output: True, if m balls can be placed with distance >= gap.
False, otherwise.
'''
next_valid_pos = position[0] + gap
# start from smallest position
good_positions = 1
for idx in range(1, size):
if position[idx] >= next_valid_pos:
good_positions += 1
next_valid_pos = position[idx] + gap
if good_positions == m:
return True
return False
# --------------------------------------------
# preprocessing, keep it sorted in ascending order
position.sort()
# maximum gap between two balls
max_gap = (position[-1] - position[0]) // (m - 1)
# minimum gap between two balls
min_gap = min( position[i] - position[i-1] for i in range(1, size) )
if m == 2:
# Quick response for simple case
return max_gap
# launch binary search to find optimal gap
left, right = min_gap, max_gap
while left <= right:
gap_trial = left + (right - left) // 2
if satisfy(gap=gap_trial):
# m balls can be placed with gap_trial
# make gap_trial larger, and try again
left = gap_trial + 1
else:
# m balls cannot be placed with gap_trial
# make it smaller, and try again
right = gap_trial - 1
# because left = gap_trial + 1
# left - 1 is the optimal gap
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:
position.sort()
def valid(dis):
count = 1
prev = position[0]
for n in position:
if n-prev >= dis:
count += 1
prev = n
if count == m: return True
return False
l, r = 0, position[-1]
ret = 0
# print(position, l, r)
while l < r:
mid = l + (r-l)//2
# print(l, r, mid)
if valid(mid):
ret = max(ret, mid)
l = mid+1
else:
r = mid
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:
position.sort()
def trial(n):
prev, curr, ct = 0, 1, 1
while True:
if curr >= len(position):
return False
if position[curr] - position[prev] >= n:
prev = curr
ct += 1
if ct == m:
return True
curr += 1
l, r = 1, position[-1] - position[0] + 1
ret = 0
while l < r:
mid = (l + r + 1) // 2
ans = trial(mid)
if ans:
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
L = len(position)
left = 1
right = position[-1]-position[0]
def can_place(f):
n_ball = m-1
cum_dis = 0
p = 1
#n_ball -= 1
while n_ball >0 and p<L :
cum_dis += position[p]-position[p-1]
if cum_dis >= f:
n_ball -= 1
cum_dis = 0
p += 1
if n_ball == 0:
return True
else:
return False
#return can_place(4)
# 1, 9, 5 false
# 1, 4, 2, true
# 2, 4, 3, true
# 3, 4, 3,
while left < right:
mid = right- (-left+right)//2
#mid = (left+right)//2
if can_place(mid):
left = mid
else:
right = mid-1
# if right-left == 1:
# if can_place(right):
# return right
# else:
# return left
#return can_place(3)
return left
# if m == 2:
# return position[-1] - position[0]
# L = len(position)
# hp = []
# f_set = {}
# b_set = {}
# for i in range(L-1):
# heapq.heappush(hp, (position[i+1]-position[i], position[i], position[i+1]))
# f_set[position[i]] = position[i+1]
# b_set[position[i+1]] = position[i]
# total = L-1
# while total > m-1:
# l, x, y = heapq.heappop(hp)
# if x in f_set and y in b_set and f_set[x] == y and b_set[y] == x:
# left_len = float(inf)
# right_len = float(inf)
# if y in f_set:
# right_len = f_set[y]-y
# if x in b_set:
# left_len = x-b_set[x]
# if left_len < right_len:
# #merge with left
# new_y = y
# new_x = b_set[x]
# del f_set[x]
# del b_set[x]
# f_set[new_x] = new_y
# b_set[new_y] = new_x
# ret = new_y - new_x
# else:
# # merge with right
# new_y = f_set[y]
# new_x = x
# del f_set[y]
# del b_set[y]
# f_set[new_x] = new_y
# b_set[new_y] = new_x
# ret = new_y - new_x
# heapq.heappush(hp, (new_y-new_x, new_x, new_y))
# total -= 1
# ret = float(inf)
# for k in f_set.keys():
# ret = min(ret, f_set[k]-k)
# return f_set
|
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-l+1)//2
placefind = 1
preIndex = 0
for i in range(1, len(position)):
if position[i]-position[preIndex]>=mid:
placefind += 1
preIndex = i
if placefind >= m:
l = mid
else:
r = mid-1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
lo=0
hi=position[-1]-position[0]
n=len(position)
def count(d):
ans, curr = 1, position[0]
for i in range(1, n):
if position[i] - curr >= d:
ans += 1
curr = position[i]
return ans
while lo<hi:
mid= (lo+hi+1)//2
res=count(mid)
if res>=m:
lo=mid
elif res<m:
hi=mid-1
return lo
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def check(space):
count = start = 0
for i in range(1, len(position)):
if position[i] - position[start] >= space:
count += 1
start = i
print(space, count)
return count >= m - 1
def find_space(lo, hi):
while lo < hi:
mid = int((lo / 2) + (hi / 2))
if check(mid) and not check(mid + 1):
return mid
if check(mid):
lo = max(mid, lo + 1)
else:
hi = mid
hi -= 1
while check(hi):
hi += 1
return hi - 1
position.sort()
return find_space(1, position[-1] - position[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()
l, r = 1, 10**9
while l < r:
mid = (l+r+1)//2
count = 1
prev = position[0]
for i in range(1, len(position)):
if position[i] - prev >= mid:
count += 1
prev = position[i]
if count >= 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 is_feasible(force):
count = 1
cur = position[0]
for i in range(1, n):
# print(position[i], cur)
if position[i] - cur >= force:
count += 1
cur = position[i]
return count >= m
l = 1
r = position[-1] - position [0] + 1
while l < r:
med = (l + r) // 2
if is_feasible(med):
l = med + 1
else:
r = med
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:
# 二分查找
# 判断若x为最小磁力,能否分隔出m-1个间隔
def Valid(x):
ans = 0
target = position[0]
for i in range(1,len(position)):
if position[i] - target >= x:
ans += 1
target = position[i]
return ans >= m - 1
position.sort()
l,r = min(position[i + 1] - position[i] for i in range(len(position) - 1)),(position[-1] - position[0]) // (m - 1)
while l <= r:
mid = l + (r - l) // 2
# 若最小磁力可以分成m-1个间隔,则可能存在更大值,否则最小磁力值应当更小
if Valid(mid):
l = mid+1
else:
r = mid-1
return l-1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def realizable(value):
last = position[0]
placed = 1
for k in range(1, len(position)):
if position[k] - last >= value:
placed += 1
last = position[k]
return placed >= m
min_dist, max_dist = position[1] - position[0], position[-1] - position[0]
for k in range(1,len(position)):
min_dist = min(min_dist, position[k]-position[k-1])
left, right = min_dist, max_dist + 1
print(left, right)
while left < right:
mid = left + (right - left) // 2
if not realizable(mid):
right = mid
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 check_force(position, pivot, m):
anchor = position[0]
cnt = 1
for i in range(1, len(position)):
if position[i] - anchor >= pivot:
anchor = position[i]
cnt += 1
if cnt == m:
#print(\"Successfully placed all baskets\")
return True
return False
position = sorted(position)
low, high = 0, position[-1]
output = -1
# Standard binary search recipe
while low < high:
pivot = (low + high) // 2
if check_force(position, pivot, m):
output = max(output, pivot)
low = pivot + 1
else:
high = pivot
return output
|
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(space):
count = start = 0
for i in range(1, len(position)):
if position[i] - position[start] >= space:
count += 1
start = i
# print(space, count)
return count >= m - 1
def find_space(lo, hi):
while lo < hi:
mid = int((lo / 2) + (hi / 2))
if check(mid) and not check(mid + 1):
return mid
if check(mid):
lo = max(mid, lo + 1)
else:
hi = mid
hi -= 1
while check(hi):
hi += 1
return hi - 1
position.sort()
return find_space(1, position[-1] - position[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 = sorted(position)
# print('------------')
maxVal = position[-1] - position[0]
answer = 0
low, high = 1, maxVal
def isValid(mid):
pos = position[0]
elements = 1
for i in range(1, len(position)):
if (position[i] - pos >= mid):
pos = position[i]
elements += 1
if (elements == m):
return True
return False
while low <= high:
mid = (low+high)//2
# print(mid)
if isValid(mid):
answer = max(answer, mid)
low = mid + 1
else:
high = mid - 1
return answer
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
mid = max(position) // 2
gap = max(1,mid//2)
t = 0
best = 0
while True:
c = 1
last = position[0]
for i in range(1,len(position)):
if position[i] - mid >= last:
c+=1
last = position[i]
if c>=m:
best = max(best,mid)
mid = mid + gap
else:
mid = mid - gap
gap = gap//2
gap = max(1,gap)
t+=1
if t==50:
break
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:
def isPossible(arr, n, C, mid):
# Variable magnet will store count of
# magnets that got placed and
# currPosition will store the position
# of last placed magnet
magnet = 1
currPosition = arr[0]
for i in range(1, n):
# If difference between current index
# and last placed index is greater than
# or equal to mid it will allow placing
# magnet to this index
if (arr[i] - currPosition >= mid):
magnet += 1
# Now this index will become
# last placed index
currPosition = arr[i]
# If count of magnets placed becomes C
if (magnet == C):
return True
# If count of placed magnet is
# less than C then return false
return False
# Function for modified binary search
def binarySearch(n, C, arr):
# Sort the indices in ascending order
arr.sort(reverse = False)
# Minimum possible distance
lo = 0
# Maximum possible distance
hi = arr[n - 1]
ans = 0
# Run the loop until lo becomes
# greater than hi
while (lo <= hi):
mid = int((lo + hi) / 2)
# If not possibble, decrease value of hi
if (isPossible(arr, n, C, mid) == False):
hi = mid - 1
else:
# Update the answer
ans = max(ans, mid)
lo = mid + 1
# Return maximum possible distance
return ans
n = len(position)
return binarySearch(n, m, position)
|
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 cnt(mid) -> int:
ans, cur = 1, position[0]
for i in range(1, n):
if position[i] - cur >= mid:
ans += 1
cur = position[i]
return ans
l, r = 0, position[-1] - position[0]
while(l < r):
mid = r - (r - l)//2
if cnt(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 helper(position, x, m):
prev = position[0]
m -= 1
#greedy
for i in range(1, len(position)):
if position[i] - prev>= x:
prev = position[i]
m -=1
if m == 0:
break
if m == 0:
return x
else:
return -1
l = 1
r = position[-1] - position[0] + 1
ans = 0
while l < r:
mid = (l + r)//2
realMinimumForce = helper(position, mid, m)
print((mid, realMinimumForce))
if realMinimumForce == -1:
r = mid
else:
l = mid + 1
ans = max(ans,realMinimumForce)
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:
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 checkDistance(self, position, minDist, m):
lastBallPos = position[0]
ballLeft = m - 1
i = 1
while i < len(position) and ballLeft != 0:
if minDist <= position[i] - lastBallPos:
lastBallPos = position[i]
ballLeft -= 1
i += 1
return ballLeft == 0
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
high = 1000000000
low = 1
ans = 1
while low < high:
middle = (high + low + 1) // 2
if self.checkDistance(position, middle, m):
low = middle
else:
high = middle - 1
return low
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def count(d):
prev, tot = position[0], m-1
for i in range(1, len(position)):
if position[i] - prev >= d:
prev = position[i]
tot -= 1
if tot == 0: return True
return False
position.sort()
low, high = 0, position[-1]-position[0]
res = None
while low <= high:
mid = (low + high)//2
if count(mid):
res = mid
low = mid + 1
else:
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()
def helper(position, x, m):
res = float('inf')
prev = position[0]
m -= 1
for i in range(1, len(position)):
if position[i] - prev>= x:
res = min(position[i] - prev, res)
prev = position[i]
m -=1
if m == 0:
break
if m == 0:
return res
else:
return -1
l = 1
r = position[-1] - position[0] + 1
ans = 0
while l < r:
mid = (l + r)//2
realMinimumForce = helper(position, mid, m)
#print(mid, realMinimumForce)
if realMinimumForce == -1:
r = mid
else:
l = mid + 1
ans = max(ans,realMinimumForce)
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
lo, hi = 0, position[-1] - position[0]
def check(amt):
curr = position[0]
cnt = 1
for x in position[1:]:
if x - curr >= amt:
cnt += 1
curr = x
return cnt >= m
while lo < hi:
mi = (hi + lo + 1) // 2
if check(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:
def validPlacement(spacing):
placed, curPosition = 1, position[0]
for p in position:
if p - curPosition >= spacing:
placed += 1
curPosition = p
return placed >= m
## IDEA: do binary search to find the minimum foracce (maximum space)
position.sort()
lo, hi = 0, position[-1] - position[0]
while (lo < hi):
mid = hi - (hi - lo) // 2 # midpoint formula to prevent integer overflow in other languages
if validPlacement(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
|
import bisect
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
if m == 2: return position[-1] - position[0]
lp = 0
rp = (position[-1] - position[0])
def can(gap):
lidx = 0
left = m - 1
ptr = 1
while left > 0 and ptr < n:
if position[ptr] - position[lidx] >= gap:
left -= 1
lidx = ptr
ptr = lidx + 1
continue
ptr += 1
return left == 0
ans = 0
while lp < rp:
mid = (lp+rp+1)//2
if can(mid):
lp = mid
else:
rp = mid-1
return lp
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def check(min_force):
count = 1
k = position[0]
for n in position:
if n-k >= min_force:
count += 1
k = n
print(min_force, count)
if count >= m:
return True
return False
i, j = 1, position[-1]-position[0]
while i <= j:
mid = (i+j)//2
c1 = check(mid)
c2 = check(mid+1)
if c1 and not c2:
return mid
if not c1:
j = mid-1
else:
i = mid+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 search(s,e,m,l):
mid=(s+e)//2
if s>e:
return e
return Solution.search(mid+1,e,m,l) if Solution.ispossible(mid,m,l) else Solution.search(s,mid-1,m,l)
def ispossible(n, m, l):
count=1
p=l[0]
for i in range(1,len(l)):
if l[i]-p >= n:
count+=1
p=l[i]
if count>=m:
return True
return False
def maxDistance(self, position: List[int], m: int) -> int:
position = sorted(position)
return Solution.search(1,position[-1]-position[0], m, position)
|
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 check(self,position,mid,m):
prev=position[0]
rem=m-1
i=1
while i<len(position) and rem:
if position[i]-prev<mid:
i+=1
else:
rem-=1
prev=position[i]
return rem==0
def maxDistance(self, position: List[int], m: int) -> int:
l=1
h=1000000000
position.sort()
while l<h:
mid=(l+h+1)//2
if self.check(position,mid,m):
l=mid
else:
h=mid-1
return l
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.