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 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()
print(position)
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
print(high, low)
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 can_place(force):
count = 1
curr = position[0]
for i in range(1, len(position)):
if position[i] - curr >= force:
count += 1
curr = position[i]
return count
position.sort()
lo, hi = 0, position[-1] - position[0]
while lo < hi:
mid = (lo + hi + 1) // 2
if can_place(mid) >= m:
lo = mid
else:
hi = mid - 1
return lo
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
left = 1
right = (position[-1]-position[0]) // (m-1) + 1
while left<right:
mid = (left+right)//2
if self.can(position, mid, m):
left = mid + 1
else:
right = mid
return left if self.can(position, left, m) else left-1
def can(self, position, mid, m):
n = len(position)
pos = [0 for i in range(m)]
i = 1
j = 1
while i < n and j < m:
if position[i] - position[pos[j-1]] >= mid:
pos[j] = i
j += 1
i += 1
if j==m:
return True
return False
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: 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 check(self,position,m,mid):
lastpos=position[0]
rem=m-1
i=1
while i<len(position) and rem:
if position[i]-lastpos<mid:
i+=1
else:
rem-=1
lastpos=position[i]
return rem==0
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
low=1
high=10**9
while low<high:
mid=low+(high-low)//2
if self.check(position,m,mid):
low=mid+1
else:
high=mid
return low-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:
# binary search O(NlogQ), n = len(position), q = max(position)
position.sort()
def maxBalls(d):
# O(N), if distance >= d, how many magnetic balls can be placed?
count, p = 0, -d
for x in position:
if x - p >= d:
count, p = count + 1, x
return count
l, r = 0, max(position)
while l < r:
d = r - (r - l) // 2
if maxBalls(d) >= m:
l = d
else:
r = d - 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()
# all_dists = set()
# for i in range(len(position) - 1):
# for j in range(i + 1, len(position)):
# all_dists.add(position[j] - position[i])
# all_dists_list = list(all_dists)
# all_dists_list.sort(reverse=True)
lo = 1
hi = position[-1]
while lo < hi:
dist = (hi + lo) // 2 + 1
if self.maxBalls(position, dist) >= m:
lo = dist
else:
hi = dist - 1
# print(lo, hi)
return lo
def maxBalls(self, sorted_position: List[int], distance: int) -> int:
l_ind = 0
r_ind = 1
count = 1
while r_ind < len(sorted_position):
if sorted_position[r_ind] - sorted_position[l_ind] >= distance:
count += 1
l_ind = r_ind
r_ind += 1
return count
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
# print(position)
high=int((position[-1]-position[0])/(m-1))
low=0
while high-1>low:
mid=int((high+low)/2)
# print('high:'+str(high)+'low:'+str(low)+'mid:'+str(mid))
if self.helper(position,mid,m):
# print('True')
low=mid
else:
# print('False')
high=mid-1
if self.helper(position,high,m):
return high
else:
return low
def helper(self,position,max_d,m):
count=1
pre_ind=0
ind=1
while ind<len(position):
if count>=m:
break
# print('ind:'+str(ind)+'count:'+str(count))
if position[ind]-position[pre_ind]<max_d:
ind+=1
else:
pre_ind=ind
ind+=1
count+=1
if count>=m:
return True
else:
return False
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
lo, hi = 1, 10**9
while hi>lo:
mid = (lo+hi+1)//2
lapo = position[0]
co = 1
for po in position[1:]:
if po-lapo >= mid:
lapo = po
co += 1
if co >= m:
lo = mid
else:
hi = mid-1
return lo
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
def count(d):
res = 1
curr = position[0]
for i in range(1,n):
if position[i] - curr >= d:
res += 1
curr = position[i]
return res
l = 0
r = position[-1] - position[0]
while l < r:
mid = (l + r + 1) // 2
if count(mid) >= m:
l = mid
else:
r = mid - 1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
arr = sorted(position)
if m == 2:
return arr[-1] - arr[0]
def check(n):
cnt = 2
pre = arr[0]
for i in range(1, len(arr) - 1):
if arr[i] - pre >= n:
pre = arr[i]
cnt += 1
if cnt == m:
break
return cnt == m and arr[-1] - pre >= n
lo, hi = 1, arr[-1] - arr[0]
while lo <= hi:
mi = lo + (hi - lo) // 2
if check(mi):
lo = mi + 1
else:
hi = mi - 1
return hi
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position = sorted(position)
def is_good(position, target_distance, m):
prev = position[0]
count = 1
for i in range(1, len(position)):
pos = position[i]
if pos - prev >= target_distance:
prev = pos
count += 1
if count == m:
return True
return False
left = 1
right = max(position)
while right > left + 1:
mid = (left+right)//2
if is_good(position, mid, m):
left = mid
else:
right = mid
if is_good(position, right, m):
return right
else:
return left
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def count(d):
curr = position[0]
ans = 1
for i in range(1, len(position)):
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
c = count(mid)
if c >= 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:
minp,maxp = min(position),max(position)
if m == 2:
return maxp-minp
position.sort()
def valid(i):
count = 1
ending = position[0]
for j in range(1,len(position)):
if position[j] >= ending+i:
count += 1
ending = position[j]
return True if count >= m else False
left,right = 0, maxp-minp+1
while right-left > 1:
#print(left,right)
mid = left+(right-left)//2
if valid(mid):
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, 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, pp: List[int], k: int) -> int:
n=len(pp)
pp.sort()
l=1
r=pp[-1]-pp[0]+1
def ok(m):
i=1
pre=pp[0]
for _ in range(k-1):
while i<n and pp[i]-pre<m:i+=1
if n==i:return False
pre=pp[i]
i+=1
return True
while l+1<r:
m=(l+r)//2
if ok(m):l=m
else:r=m
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 foo( d):
ans = 1; last = position[0]
for i in range( 1, len( position)):
if position[i] - last >= d:
ans += 1
last = position[i]
return ans
left = 0; right = position[-1] - position[0]
while left < right:
mid = right - ( right - left) // 2
if foo( mid) >= m:
left = mid
else:
right = mid - 1
return left
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
def check(d):
k, pre = 1, position[0]
for i in range(1, n):
if position[i] - pre >= d:
k += 1
pre = position[i]
return k >= m
lo, hi = 0, position[-1] - position[0]
while lo < hi:
mid = hi - (hi - lo) // 2
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:
position.sort()
def can_reach(force):
last = position[0]
placed = 1
for i in range(1, len(position)):
pos = position[i]
if pos - last >= force:
last = pos
placed += 1
if placed >= m:
break
return placed >= m
l = 1
r = position[-1] - position[0] + 1
while l < r:
mid = (l+r)//2
if not can_reach(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()
l, r = 0, 10**9 + 2
while l < r:
mid = (l + r) // 2
prev = position[0]
cnt = 1
for p in position:
if p >= prev + mid:
prev = p
cnt += 1
# print(l, r, mid, cnt)
if cnt < m:
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:
def c(p,m,d):
b=m-1
l=len(p)
acc=0
for i in range(1,l):
acc+=p[i]-p[i-1]
if acc>=d:
acc=0
b-=1
if b==0:
return True
return False
low=1
high=1000000000
mid=0
position.sort()
while low<=high:
mid=(low+high)//2
if c(position,m,mid):
low=mid+1
else:
high=mid-1
return high
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def possible(num):
Count, pos = 1, 0
for i in range(1, len(position)):
if position[i] - position[pos] >= num:
pos = i
Count += 1
if Count==m:
return True
return Count >= m
def search(left,right):
if left == right:
return left
mid = (left + right)//2
if possible(mid) and not possible(mid + 1):
return mid
elif possible(mid):
return search(mid + 1, right)
return search(left, mid - 1)
return search(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, positions: List[int], m: int) -> int:
positions.sort()
# try to add balls and record the position
def check(mid):
p = 0
count = 0
for q in range(len(positions)):
if positions[q] - positions[p] >= mid:
p = q
count += 1
return count >= m-1
low, high = 0, positions[-1]-positions[0]
# print(low, high)
res = 0
while low <= high:
mid = (low + high)//2
if check(mid):
low = mid + 1
res = max(res, mid)
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:
# def feasible(distance):
# balls, curr = 1, position[0]
# for i in range(1, len(position)):
# if position[i]-curr >= distance:
# balls += 1
# curr = position[i]
# return balls >= m
# position.sort()
# left, right = 0, position[-1]-position[0]
# while left<right:
# mid = left + (right-left)//2
# if feasible(mid):
# right = mid
# else:
# left = mid+1
# return left
n = len(position)
position.sort()
def count(d):
ans, curr = 1, position[0]
for i in range(1, n):
if position[i] - curr >= d:
ans += 1
curr = position[i]
return ans
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:
lo = 1
hi = 10**18
position.sort()
def func(mid):
cnt = 1
cur = position[0]
for i in range(1,len(position)):
if position[i]>=cur+mid:
cur = position[i]
cnt+=1
if cnt>=m:
return True
else:
return False
while lo<hi:
mid = (lo+hi)//2
x = func(mid)
if x==True:
lo = mid
else:
hi = mid
if hi-lo==1:
break
if func(hi)==True:
return hi
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 helper(pos, mid, k):
temp = pos[0]
done = 1
for i in range(1, len(pos)):
if pos[i] - temp >= mid:
temp = pos[i]
done += 1
if done == k:
return True
return False
position.sort()
res = -1
l, r = 0, position[-1]
while l < r:
mid = l + (r - l) // 2
if helper(position, 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:
n = len(position)
position = sorted(position)
m_vals = []
distance = position[-1] - position[0]
def works(min_dist):
i = 1
count = 1
prev = 0
while count < m and i < n:
if position[i] - position[prev] >= min_dist:
count += 1
prev = i
i += 1
return count == m
low = 0
high = 1 << 32
while high - low > 1:
mid = (low + high) // 2
if works(mid):
low = mid
else:
high = mid - 1
if works(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, positions: List[int], m: int) -> int:
positions.sort()
def valid(mid):
count = 0
last = -math.inf
for pos in positions:
if pos >= last + mid:
count += 1
last = pos
return count >= m
left, right = 0, positions[-1]
while left <= right:
mid = (left + right) // 2
if valid(mid):
left = mid + 1
else:
right = mid - 1
return right
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
# class Solution:
# def maxDistance(self, position: List[int], m: int) -> int:
# position.sort()
# def check(d):
# k = 1
# pre = position[0]
# for i in range(1, len(position)):
# if position[i] - pre >= d:
# k += 1
# pre = position[i]
# return k >= m
# lo, hi = 0, position[-1] - position[0]
# while lo < hi:
# mid = (lo + hi)//2
# if check(mid):
# lo = mid
# else:
# hi = mid - 1
# return lo
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 isSafe(self,lst,m,key):
n = len(lst)
pre = lst[0]
index = 1
while(index<n):
while(index<n and lst[index]-pre<key):
index+=1
m-=1
if(m==0):
return True
if(index==n):
return False
pre = lst[index]
return False
def maxDistance(self, lst: List[int], m: int) -> int:
lst.sort()
n = len(lst)
low = 0
high = lst[-1] - lst[0]
ans = 0
while(low<=high):
mid = (low+high)//2
if(self.isSafe(lst,m,mid)):
ans = mid
low = mid + 1
else:
high = mid - 1
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, 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 = float('inf')
for i in range(n-1):
l = min(l, pos[i+1] - pos[i])
r = pos[-1] - pos[0] + 1
while l < r:
mid = (l + r) // 2
if check(mid):
r = mid
else:
l = mid + 1
return l - 1
# if check(l):
# return l
# else:
# return l - 1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
import numpy as np
import itertools as it
class Solution:
# # def maxDistance(self, position: List[int], m: int) -> int:
# arr = np.sort(position)
# md = max(arr)
# # print(arr)
# combList = list(it.combinations(arr, m))
# # print(combList)
# maxmindiff = 0
# targetList = []
# for comb in combList:
# cb = list(it.combinations(comb, 2))
# minDiff = md
# for k in range(len(cb)):
# curDiff = abs(cb[k][0]-cb[k][1])
# if curDiff< minDiff:
# minDiff= curDiff
# if minDiff>maxmindiff:
# maxmindiff = minDiff
# targetList = comb
# # print(maxmindiff)
# # print(targetList)
# return maxmindiff
def maxDistance(self, position: List[int], m: int) -> int:
arr = np.sort(position)
arr = arr -np.min(arr)
print(arr)
def isFeasible(mid, arr, m):
pos = arr[0]
n = len(arr)
count = 1
for i in range(n):
if arr[i] - pos>=mid:
pos = arr[i]
count+=1
if count>=m:
return True
return False
left = arr[0]
right = arr[-1]
if m ==2:
return right-left
res = -1
count = 0
while left < right:
# mid = np.ceil((right+left)/2)
mid = (right+left)//2
# mid = right-(right-left)//2
# print(mid)
count +=1
if isFeasible(mid, arr, m):
res = max(res,mid)
left = mid +1
else:
right = mid
print(count)
return int(res)
############################3
# 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:
position.sort()
n = len(position)
low, high = 1, 10 ** 9
answer = 0
while low <= high:
mid = (low + high) >> 1
cnt = 0
temp = -(10 ** 9)
for i in position:
if i - temp >= mid:
cnt += 1
temp = i
if cnt >= m:
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 fun(self, arr, dist):
pos = arr[0]
cows = 1
for i in range(1, len(arr)):
e = arr[i]
if e-pos >= dist:
pos = e
cows += 1
if cows == self.m: return True
return False
def bs(self, arr):
lef = 0
rit = arr[-1]
while lef < rit:
mid = (lef+rit)//2
# print('mid, mid+1', mid, mid+1)
me = self.fun(arr, mid)
nex = self.fun(arr, mid+1)
# print('me, nex', me, nex)
if me and not nex:
return mid
elif me and nex:
lef = mid + 1
else:
rit = mid - 1
# print('lef, rit', lef, rit)
return lef
def maxDistance(self, position: List[int], m: int) -> int:
self.m = m
position.sort()
return self.bs(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:
def isFeasible(positions, mid, k):
taken = 1
last = positions[0]
for i in range(1, len(positions)):
if positions[i] - last >= mid:
taken += 1
last = positions[i]
return taken >= k
def largestMinDist(positions, n, k):
low = 0
high = max(positions) - min(positions) + 1
while high - low > 1:
mid = (low + high) // 2
if isFeasible(positions, mid, k):
low = mid
else:
high = mid
return low
n = len(position)
position = sorted(position)
'''
if m == len(position):
ans = max(position) - min(position)
for i in range(1, n):
diff = position[i] - position[i - 1]
if diff < ans:
ans = diff
return ans
'''
return largestMinDist(position, n, m)
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
l=1
r=position[-1]
pos=0
print((l,r))
def check(mid):
cow_cnt=1
last_pos=position[0]
for i in range(1,len(position)):
if position[i]-last_pos>=mid:
cow_cnt+=1
if cow_cnt==m:
return True
last_pos=position[i]
return False
while l<=r:
mid=l+(r-l)//2
if check(mid):
pos=mid
l=mid+1
# print(mid)
else:
r=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, position: List[int], m: int) -> int:
def check(pos):
placed = 1
i = 0
j = 1
while j < l:
if position[j] - position[i] >= pos:
placed +=1
if placed == m:
return True
i = j
j = i + 1
else:
j +=1
return (True if placed == True else False)
l = len(position)
mx = max(position) - min(position)
position.sort()
lo = 1
hi = mx
ans = -1
while lo <= hi:
mid = lo + (hi - lo) // 2
if check(mid):
ans = mid
lo = mid + 1
else:
hi = mid - 1
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def check(dist):
balls = m
prev = None
res = float('inf')
for p in position:
if prev is None or p - prev >= dist:
if prev:
res = min(res, p - prev)
prev = p
balls -= 1
if balls > 0:
return None
return res
lo, hi = 1, (position[-1] - position[0]) // (m - 1)
res = 0
# print('lohi', lo, hi)
while lo <= hi:
mid = (lo + hi) // 2
r = check(mid)
# print('dist', mid, res)
if r:
res = r
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, position: List[int], m: int) -> int:
position.sort()
mn = sys.maxsize
mx = position[-1] - position[0]
for i in range(1, len(position)):
mn = min(mn, position[i] - position[i-1])
def isPoss(diff):
count = 1
start = 0
cd = 0
f = sys.maxsize
for i in range(1, len(position)):
if cd + position[i] - position[i-1] < diff:
cd += position[i] - position[i-1]
else:
count += 1
f = min(f, position[i] - position[start])
start = i
cd = 0
if count == m:
break
if count == m:
return f
return -1
ans = -1
while mn <= mx:
mid = (mn + mx)//2
v = isPoss(mid)
if v != -1:
ans = v
mn = mid + 1
else:
mx = 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
|
import bisect
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
def possible(gap):
prev = position[0]
idx = 0
left = m - 1
while left > 0:
idx = bisect.bisect_left(position, prev+gap, idx + 1, n)
if idx >= n:
return False
else:
prev = position[idx]
left -= 1
return True
position.sort()
min_gap = 1
max_gap = position[-1] - position[0]
while min_gap < max_gap:
cur_gap = (min_gap + max_gap + 1) // 2
if possible(cur_gap):
min_gap = cur_gap
else:
max_gap = cur_gap - 1
return min_gap
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position, m):
position.sort()
n = len(position)
def canFit(gap):
ct = 1
i = 0
j = 1
while j < n:
while j<n and position[j]-position[i] < gap:
j += 1
# print(i, j, ct)
if j<n:
ct += 1
i = j
j += 1
if ct == m:
return True
return False
# print(canFit(999999999))
lo = 1
hi = position[-1] - position[0]
res = 0
ct = 0
while lo<=hi:
mid = (lo + hi) >> 1
ct += 1
if canFit(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, position: List[int], m: int) -> int:
if len(position) < m: return 0
position.sort()
def can(force):
cnt = 1
pos = 0
for i in range(1, len(position)):
if position[i] - position[pos] >= force:
cnt += 1
pos = i
return cnt >= m
low, high = 0, position[-1] - position[0]
while low < high:
mid = (low + high + 1) // 2
if can(mid):
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()
def feasible(dist, m):
pre_pos = position[0]
placed = 1
for i in range(1, len(position)):
if position[i] - pre_pos >= dist:
placed +=1
if placed >= m:
return True
pre_pos = position[i]
return False
l = 1
r = position[-1] - position[0]
while l <= r:
mid = l + (r - l) // 2
if feasible(mid, m):
l = mid + 1
else:
r = mid - 1
return l-1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: 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 possible(min_diff,arr,m):
start = 0
total = 1
end = 1
while end < len(arr):
if arr[end]-arr[start] >= min_diff:
total += 1
start = end
end += 1
if total >= m:
return True
return False
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
ans = -1
low = 1
high = 10**9
while low <= high:
mid = (low+high)//2
if possible(mid,position,m):
low = mid+1
ans = mid
else:
high = mid-1
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position = sorted(position)
i,j = 1,1000000001
ans = 0
while i<=j :
prevCow = position[0]
mid = (i+j)//2
currCows = 1
for k in range(1,len(position)) :
if position[k] - prevCow >= mid :
currCows += 1
prevCow = position[k]
if currCows < m :
j = mid - 1
else :
i = mid + 1
ans = 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 valid(interval,m):
last=-interval
for p in position:
if p-last>=interval:
m-=1
last=p
return True if m<=0 else False
#binary search interval: return the maxiumal interval
lo,hi=1,max(position)//(m-1) # the interval cannot be than the ideal condition
while lo<=hi:
mid=lo+(hi-lo)//2
if valid(mid,m):# the correct value can be bigger
lo=mid+1#[min,lo-1] is valid all the time
else:
hi=mid-1#[hi+1,max] is not valid all the time
# lo=hi+1; [min,lo-1] is valid, [lo,max] is not valid
return hi
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position = sorted(position)
def count(d):
cnt = 1
cur = position[0]
for p in position[1:]:
if p>=cur+d: cur, cnt = p, cnt+1
return cnt
l, r = 0, position[-1]-position[0]
while l<r:
mid = r - (r-l)//2
# count_mid = count(mid)
# if count(mid) >= m: return r-l
if count(mid) < m:
r = mid-1
else:
l = mid
# print(mid, count(mid))
return r
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def 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 = position[-1]
low = 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 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
position = sorted(position)
if m == 2:
return position[-1] - position[0]
dist = []
for i in range(len(position) -1):
dist.append(position[i+1] - position[i])
if m == len(position):
return min(dist)
left = 0
right = position[-1]
res = 1
while left < right:
mid = (left + right) // 2
if isFeasible(mid, position, len(position), 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:
import bisect
def maxDistance(self, position: List[int], m: int) -> int:
l, r = 1, max(position)-min(position)
if m == 2: return r
position = sorted(position)
def fea(D):
#print(\" \",D)
s = min(position)
need = 1
last = 1
while(True):
for i in range(last,len(position)):
if position[i]>=s+D:
need += 1
s = position[i]
last = i
break
if last == len(position)-1 or position[-1]<s+D:
break
#print(\" \",need)
return need
print((fea(2)))
while(l<r):
mid = l+(r-l)//2
if fea(mid)<m:
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()
n = len(position)
# distances = [i for i in range(10**9)]
# distances.sort()
hi = 10**9-1
li = 0
while li <= hi:
# print(\"li \",li,\"hi \",hi)
if li == hi:
return li
mid = (hi+li)//2
if hi == li+1:
mid += 1
d = mid
i = 0
j = 0
count = 1
flag = True
while count < m:
while position[j]<position[i] + d:
j += 1
if j == n and count < m:
flag = False
break
if j == n and count <m:
flag = False
break
i = j
count += 1
if hi == li+1:
if flag:
return hi
else:
return li
if flag:
li = mid
else:
hi = 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, arr: List[int], m: int) -> int:
arr = sorted(arr)
low, high = 1, arr[-1]-arr[0]+1
def isValid(k, balls):
balls-=1
prev = arr[0]
res = float('inf')
for i in range(1,len(arr)):
if arr[i]-prev >= k:
res = min(res, arr[i]-prev)
balls-=1
prev = arr[i]
if balls == 0: break
return res if balls == 0 else -1
ans = 0
while low < high:
mid = low + (high-low)//2
tmp = isValid(mid, m)
if tmp != -1:
ans = tmp
low = mid+1
else:
high = 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, pos: List[int], m: int) -> int:
pos = sorted(pos)
def test(gap, pos = pos, m = m):
cur = pos[0]
i = 1
m -= 1
while i < len(pos):
if pos[i] - cur >= gap:
cur = pos[i]
m -= 1
if m == 0: return True
i += 1
return False
lower, upper = 1, pos[-1]-pos[0]
if test(upper): return upper
while True:
gap = (lower + upper) // 2
if test(gap): lower = gap
else: upper = gap
if upper - lower == 1: return lower
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
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:
if m < 2: return -1
pos.sort()
avg = (pos[-1]-pos[0]+1)//(m-1)+1
#boolean
def check(f):
nonlocal pos, m
cnt = 0
pidx = 0
for i in range(1, len(pos)):
if (pos[i]-pos[pidx]+1) >= f:
cnt += 1
pidx = i
#print(\"cnt:\", cnt)
return cnt >= m-1
l, r = 1, avg
#print(\"l:\", l, \"r:\", r)
while l+1<r:
mid = l+(r-l)//2
if check(mid):
l = mid
else:
r = mid-1
#print(\"l:\", l, \"r:\", r)
if check(r): return r-1
else: return l-1
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
lo = 1
hi = position[-1] - position[0]
res = 0
while lo <= hi:
mid = lo + (hi - lo) // 2
count = 1
prev = position[0]
for i in range(1, n):
if position[i] - prev >= mid:
count += 1
prev = position[i]
if count >= m:
res = max(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, pos: List[int], m: int) -> int:
pos.sort()
low = 0
high = pos[-1] - pos[0]
def chk(d):
nonlocal m
n = len(pos)
c = m
p = 0
for i in range(n):
if c == 0:
return True
if i == 0:
c -= 1
p = pos[0]
continue
if pos[i] - p >= d:
c -= 1
p = pos[i]
return c <= 0
while low < high:
mid = int( math.ceil( (low + high) / 2 ) )
if chk(mid):
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:
start = 0
position.sort()
end = position[-1]
def good(x):
s = -1e100
count = 0
for p in position:
if p - s >= x:
count += 1
s = p
return count >= m
while start < end:
mid = (start + end) // 2
if good(mid + 1):
start = mid + 1
else:
end = mid
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:
def count(dist):
prev, balls = -1e9, 0
for p in position:
if p - prev >= dist:
balls += 1
if balls == m:
break
prev = p
return balls
position.sort()
l, r = 1, position[-1]
res = 0
while l < r:
mid = (l + r) // 2
balls = count(mid)
if balls == m:
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()
if m == 2:
return position[-1] - position[0]
else:
maxd = position[-1] // (m-1)
start = 1
end = maxd
while start < end:
middle = (start+end) // 2
result = self.is_satisfy(position, middle, m)
if result:
if start == middle:
result2= self.is_satisfy(position, middle+1, m)
if result2:
return middle + 1
else:
return middle
start = middle
else:
end = middle - 1
return start
def is_satisfy(self, position, maxd, m):
pre_postion = position[0]
index = 1
left_count = m-1
pcount = len(position)
while index < pcount:
if position[index] - pre_postion < maxd:
index += 1
continue
left_count -= 1
if left_count == 0:
return True
pre_postion = position[index]
index+=1
return False
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
l,r = 1, position[-1]
while l < r:
mid = ( r + l +1 ) //2
cnter = 0
lst = - (2*mid)
for i in position:
if i - lst >= mid:
cnter += 1
lst = i
if cnter >= 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:
# [22,57,74,79]
def possibleForce (self,force, position):
count = 0
lastBall = 0
for i in range (0,len(position)):
if (i == 0 or (position[i]- position[lastBall]) >= force):
count +=1
lastBall =i
return count
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
#print(position)
maxReally = (position[-1] -position[0] )/ (m-1)
#print (maxReally)
maxForce = maxReally
minForce = 1
result = -1
while (minForce <= maxForce and minForce >0 and maxForce <= maxReally):
middleForce = minForce + (maxForce - minForce) // 2
print((minForce, maxForce, middleForce))
count = self.possibleForce(middleForce, position)
print (count)
if count >= m:
# go right
minForce = middleForce + 1
result = middleForce
else:
# go left
maxForce = middleForce - 1
return int(result )
#x o o x o o x - o x
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
result = 0
# can we place m balls each separated by x distance?
def check(x) -> bool:
nonlocal position, m
last = position[0]
placed = 1
for pos in position:
if pos - last >= x:
placed += 1
if placed >= m:
return True
last = pos
return False
first = 0
last = int(ceil((position[-1] - position[0])/(m-1)))
while first < last:
mid = (first + last + 1)//2
if check(mid):
result = mid
first = mid
else:
last = mid - 1
return first
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
left = 1
right = position[-1] - position[0]
def countBalls(dist):
curr, balls = position[0], 1;
for i in position:
if (i-curr >= dist):
balls += 1
curr = i
return balls
while (left <= right):
mid = (left + right) // 2
if (countBalls(mid) >= m):
left = mid + 1
else:
right = mid - 1
return right
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
l = 0
r = 1 + position[-1] - position[0]
while l + 1 < r:
med = (l + r) // 2
cnt = 0
pre = position[0] - 2*med
for x in position:
if x - pre >= med:
pre = x
cnt += 1
if cnt >= m: l = med
else: r = med
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:
positions = sorted(position)
l, r = 1, positions[len(position) - 1] - positions[0]
while l < r:
mid = math.ceil((l + r) / 2)
if self.balls(positions, mid) >= m:
l = mid
else:
r = mid - 1
return l
def balls(self, positions, d):
curr, ans = -10 ** 10, 0
for position in positions:
if position - curr >= d:
curr = position
ans += 1
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
if m==2:
return position[-1]-position[0]
else:
l=1;r=position[-1]
while l<r:
mid=(l+r)//2
prev,balls=-1000000000,0
for p in position:
if p-prev>=mid:
balls+=1
if balls==m: break
prev=p
if balls==m:
ans=mid
l=mid+1
else: r=mid
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def count(dist):
prev, balls = position[0], 1
for p in position[1:]:
if p - prev >= dist:
balls += 1
if balls == m:
break
prev = p
return balls
l, r = 1, position[-1]
res = 0
while l < r:
mid = (l + r) // 2
balls = count(mid)
if balls == m:
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
|
from bisect import bisect_left
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
# log(D) * log(N) * M
n = len(position)
position.sort()
left, right = 1, (position[-1]-position[0])//(m-1)
ans = 1
while left <= right:
mid = (left+right)//2
#print(left, right, mid)
head = 0
remains = m-1
while remains > 0:
target = position[head] + mid
if target > position[-1]:
break
head = bisect_left(position, target, head)
remains -= 1
if remains == 0:
#ans = mid
left = mid+1
else:
right = mid-1
return right
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
position.sort()
def count(d):
ans, curr = 1, position[0]
for x in position[1:]:
if x - curr >= d:
ans, curr = ans+1, x
return ans
l, r = 1, position[-1] - position[0] + 1
while l < r:
p = l + (r - l) // 2
if count(p) >= m: l = p+1
else: r = p
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()
if m == 2:
return position[-1] - position[0]
def count(dist):
prev, balls = position[0], 1
for p in position[1:]:
if p - prev >= dist:
balls += 1
if balls == m:
break
prev = p
return balls
l, r = 1, position[-1]
res = 0
while l < r:
mid = (l + r) // 2
balls = count(mid)
if balls == m:
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:
# def pd(i, m):
# if m == 1:
# return float(\"inf\")
# if memo[i][m]:
# return memo[i][m]
# res = 0
# for j in range(i+1, len(position)):
# res = max(res, min(position[j] - position[i], pd(j, m-1)))
# memo[i][m] = res
# return res
# position = sorted(position)
# memo = [(1 + m) * [0] for _ in range(1 + len(position))]
# res = pd(0, m)
position = sorted(position)
if m == 2:
return position[-1] - position[0]
def solve(threshold, m):
last_ball_pos = position[0]
tot = 1
for pos in position:
if pos - last_ball_pos >= threshold:
tot += 1
last_ball_pos = pos
return tot >= m
start = 0
end = position[-1] - position[0]
res = 0
while start <= end:
middle = start + (end - start) // 2
if solve(middle, m):
start = middle + 1
res = max(res, middle)
else:
end = middle - 1
return res
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
position.sort()
maxForce = (position[-1] - position[0]) // (m - 1)
minForce = 1
while maxForce > minForce:
maxMinForce = (maxForce + minForce + 1) // 2
#print(minForce, maxForce, maxMinForce)
if self.canPut(position, m, maxMinForce):
minForce = maxMinForce
else:
maxForce = maxMinForce - 1
return minForce
def canPut(self, position: List[int], m: int, force: int) -> bool:
putCnt = 0
prePos = -1
for pos in position:
if prePos == -1 or pos - prePos >= force:
putCnt += 1
prePos = pos
if putCnt == m:
return True
return False
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position = sorted(position)
max_possible = (position[-1] - position[0]) // (m - 1)
right = max_possible
left = 0
mid = max_possible // 2
while left <= right:
if self.check(position, m, mid):
left = mid + 1
mid = (left + right) // 2
else:
right = mid - 1
mid = (left + right) // 2
return mid
def check(self, position, m, force):
p = 1
m -= 1
prev_pos = position[0]
while p < len(position):
if position[p] - prev_pos >= force:
m -= 1
prev_pos = position[p]
if not m:
return True
p += 1
return False
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
left = 1
right = position[-1] - position[0]
def countBalls(dist):
curr, balls = position[0], 1;
for i in position:
if (i-curr >= dist):
balls += 1
curr = i
return balls
while (left <= right):
mid = (left + right) // 2
if (countBalls(mid) >= m):
left = mid + 1
else:
right = mid - 1
return right
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
l, r = 1, position[-1] - position[0]
def isValid(val):
ball = 1
previous = position[0]
for p in position:
if p - previous < val:
continue
ball += 1
previous = p
return ball >= m
while l < r:
mid = l + (r - l + 1) // 2
if isValid(mid):
l = mid
else:
r = mid - 1
return r
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
if m == 2: return position[-1] - position[0]
l, r = 1, position[-1]
ans = 0
while l < r:
mid = (l + r) // 2
prev, balls = -1000000000, 0
for p in position:
if p - prev >= mid:
balls += 1
if balls == m: break
prev = p
if balls == m:
ans = mid
l = mid + 1
else:
r = mid
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
if m == 2: return position[-1] - position[0]
l, r = 1, position[-1]
ans = 0
while l < r:
mid = (l + r) // 2
prev, balls = -1000000000, 0
for p in position:
if p - prev >= mid:
balls += 1
if balls == m: break
prev = p
if balls == m:
ans = mid
l = mid + 1
else:
r = mid
return ans
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, a: List[int], m: int) -> int:
def check(d):
x, k = 0, m
for i in range(len(a)):
if a[i] >= x:
x, k = a[i] + d, k - 1
if k == 0: break
return k == 0
a.sort()
l, r = 1, (max(a) - min(a)) // (m-1)
while l + 1 < r:
d = (l + r) // 2
if check(d): l = d
else: r = d - 1
return r if check(r) else l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
res = 0
l, r = 1, (position[-1] - position[0]) // (m - 1) + 1
while l < r:
mid = (l + r) // 2
prev = position[0]
ball = 1
for i in position[1:]:
if i - prev >= mid:
ball += 1
if ball == m:
break
prev = i
if ball == m:
l = mid + 1
res = mid
else:
r = mid
return res
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def ball_count(distance):
prev,cnt=position[0],1
for pos in position[1:]:
if pos-prev>=distance:
cnt+=1
prev=pos
return cnt
position.sort()
n=len(position)
l,r=0,position[-1]-position[0]
while l<r:
mid=r-(r-l)//2
if ball_count(mid)>=m:
l=mid
else:
r=mid-1
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
import bisect
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position_set = set(position)
position.sort()
max_force = (position[-1] - position[0])//(m-1)
min_force = 1
last_idx = len(position)-1
while min_force != max_force:
mid_force = (min_force + max_force + 1) // 2
first_ball = next_ball = position[0]
next_idx = 1
for _ in range(m-2):
next_ball = first_ball+mid_force
if next_ball not in position_set:
# search the next larger
next_idx = bisect.bisect_left(position, next_ball, next_idx, last_idx)
next_ball = position[next_idx]
if next_idx == last_idx:
break
next_idx += 1
first_ball = next_ball
else:
if position[-1] - next_ball >= mid_force:
min_force = mid_force
else:
max_force = mid_force-1
continue
max_force = mid_force-1
return min_force
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def check(dist, m):
last = position[0]
for i in range(1, len(position)):
if(position[i]-last >= dist):
m-=1
last=position[i]
return(m<=1)
l=0; r=position[-1]-position[0]
while(l<r):
mid=l+((r-l+1)>>1)
if(check(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, positions: List[int], m: int) -> int:
positions.sort()
def check(value) -> bool:
last = positions[0]
count = 1
for pos in positions:
if pos-last > value:
count += 1
last = pos
if count >= m:
return False
return True
left, right = 1, max(positions)
while left < right:
mid = left + (right - left) // 2
if check(mid):
right = mid
else:
left = mid + 1
return left
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
#求两球之间的最小最大值
position.sort()
l,r=0,position[-1]-position[0] #两球之间的距离
while l<r:
mid=(l+r+1)//2
count=1
last=position[0]
for a in position[1:]:
if a-last>=mid:
count+=1
last=a
if count<m:
r=mid-1
else:
l=mid
return l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def canPut(distance: int)->bool:
count = 1
curr = position[0]
for pos in position[1:]:
if pos >= curr+distance:
count += 1
curr = pos
if count >= m:
return True
return False
position.sort()
lo, hi = 1, position[-1]-position[0]
max_distance = lo
while lo <= hi:
mi = (lo+hi)//2
if canPut(mi):
lo = mi+1
max_distance = max(max_distance, mi)
else:
hi = mi-1
#print(lo, hi)
return max_distance
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, a: List[int], m: int) -> int:
def check(d):
x, k = 0, m
for i in range(len(a)):
if a[i] >= x:
x, k = a[i] + d, k - 1
if k == 0: break
return k == 0
a.sort()
l, r = 1, (max(a) - min(a)) // (m-1)
while l + 1 < r:
d = (l + r) // 2
if check(d): l = d
else: r = d - 1
return r if check(r) else l
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def working(mid: int):
prev_stall = 0
stall = 1
# print(position)
for i in range(1, m):
while (position[stall] - position[prev_stall]) < mid:
stall+=1
if stall == len(position):
return False
prev_stall = stall
return True
L = max(position)
low = 1
high = int(L/(m-1))
largest = 0
position = sorted(position)
while low <= high:
mid = int((low+high)/2)
if working(mid):
if mid < int(L/(m-1)):
low = mid + 1
else:
largest = max(largest, mid)
break
largest = max(largest, mid)
elif mid > 0:
high = mid - 1
return largest
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
n = len(position)
left,right = 0,position[-1]-position[0] // (m-1) # search space is sorted
def count_balls(force):
balls,prev = 1,position[0]
for cur in position[1:]:
if cur-prev >= force:
balls += 1
prev = cur
return balls
while left < right:
# mid = left + (right-left)//2
mid = right - (right-left) // 2
# mid = (left+right+1)//2
if count_balls(mid) >= m:
left = mid
else:
right = mid - 1
return left
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
|
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
left = 1
right = max(position)
position.sort()
while left < right:
mid = left + (right - left)//2
legal = self.evaluate(position, m, mid)
if legal:
right = mid
else:
left = mid + 1
return left - 1
def evaluate(self, position, m, force):
balls_placed = 0
prev_val = None
for val in position:
if not prev_val:
prev_val = val
balls_placed += 1
else:
if val - prev_val >= force:
balls_placed += 1
prev_val = val
if balls_placed > m-1:
return False
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 math
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
position.sort()
diff = []
for i in range(n-1):
diff.append(position[i+1]-position[i])
def check(force,diff,num):
i = 0
j = 0
curr = 0
while i < len(diff):
curr += diff[i]
if curr >= force:
j += 1
curr = 0
i += 1
return j >= num
if m == 2:
return position[n-1]-position[0]
start = 1
end = math.ceil((position[n-1]-position[0])/(m-1))
mid = (start+end)//2
ansList = []
while start < end:
if check(mid,diff,m-1):
start = mid+1
ansList.append(mid)
else:
end = mid-1
mid = (start+end)//2
if check(mid,diff,m-1):
ansList.append(mid)
return max(ansList)
|
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
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[len(position) - 1] - position[0]
def is_doable(gap):
count, pre = 0, position[0] - 2 * gap,
for element in position:
if element - pre >= gap:
pre = element
count += 1
if count == m:
return True
return False
while True:
mid = (l + r) // 2
if is_doable(mid):
if mid == position[len(position) - 1] - position[0]:
return mid
elif is_doable(mid + 1):
l = mid + 1
else:
return mid
else:
r = 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], k: int) -> int:
position.sort()
def check(m):
prev = -1e20
c = 0
for i in position:
if i - prev >= m:
prev = i
c+=1
return c>=k
l = 0
r = max(position)
while l < r:
m = (l+r+1)//2
if check(m):
l = m
else:
r = m-1
return l
|
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.
Your input will be several matchsticks the girl has, represented with their stick length. Your output will either be true or false, to represent whether you could make one square using all the matchsticks the little match girl has.
Example 1:
Input: [1,1,2,2,2]
Output: true
Explanation: You can form a square with length 2, one side of the square came two sticks with length 1.
Example 2:
Input: [3,3,3,3,4]
Output: false
Explanation: You cannot find a way to form a square with all the matchsticks.
Note:
The length sum of the given matchsticks is in the range of 0 to 10^9.
The length of the given matchstick array will not exceed 15.
|
class Solution:
def makesquare(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
if len(nums) < 4:
return False
length = sum(nums)
if length % 4:
return False
length = (int) (length / 4)
nums.sort(reverse=True)
#print(nums)
if length < nums[0]:
return False
elif length == nums[0]:
stack = list([(set([0]), 1, length, 1)])
else:
stack = list([(set([0]), 1, length - nums[0], 2)]) # (usedIndexSet, searchStartFromIndex, target, remainRounds)
while stack:
usedSet, startIndex, target, remainRounds = stack.pop()
#print(usedSet, set(range(0, len(nums))) - usedSet, target, remainRounds)
for i in range(len(nums) - 1, startIndex - 1, -1):
if i in usedSet:
continue
num = nums[i]
if num < target and i + 1 < len(nums):
stack.append((usedSet | {i}, i+1, target - num, remainRounds))
elif num == target:
if remainRounds == 0:
return True
else:
stack.append((usedSet | {i}, 1, length, remainRounds - 1))
# Else not valid path, continue
return False
|
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.
Your input will be several matchsticks the girl has, represented with their stick length. Your output will either be true or false, to represent whether you could make one square using all the matchsticks the little match girl has.
Example 1:
Input: [1,1,2,2,2]
Output: true
Explanation: You can form a square with length 2, one side of the square came two sticks with length 1.
Example 2:
Input: [3,3,3,3,4]
Output: false
Explanation: You cannot find a way to form a square with all the matchsticks.
Note:
The length sum of the given matchsticks is in the range of 0 to 10^9.
The length of the given matchstick array will not exceed 15.
|
class Solution:
def makesquare(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
# if sum(nums) % 4 != 0 or len(nums) < 4 or max(nums) > sum(nums) / 4 :
# return False
# nums.sort(reverse=True)
# if nums[0] < sum(nums) / 4 and nums[0] + nums[-1] > sum(nums):
# return False
# def dfs(nums, pos, target):
# if pos == len(nums):
# return True
# for i in range(4):
# if target[i] >= nums[pos]:
# target[i] -= nums[pos]
# if dfs(nums, pos + 1, target):
# return True
# target[i] += nums[pos]
# return False
# return dfs(nums, 0, [sum(nums) / 4] * 4)
total = sum(nums)
if total%4 != 0 or len(nums)<4: return False
size = total/4
nums.sort(reverse=True)
used = [False]*len(nums)
def dfs(i, expect):
if i >= len(nums): return expect%size == 0
if used[i]: return dfs(i+1, expect)
used[i] = True
if nums[i] == expect: return True
if nums[i] < expect:
expect -= nums[i]
available = [j for j in range(i+1, len(nums)) if not used[j]]
for x in available:
if dfs(x, expect):
return True
used[i] = False
return False
for i in range(len(nums)):
if not dfs(i, size): return False
return True
|
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.
Your input will be several matchsticks the girl has, represented with their stick length. Your output will either be true or false, to represent whether you could make one square using all the matchsticks the little match girl has.
Example 1:
Input: [1,1,2,2,2]
Output: true
Explanation: You can form a square with length 2, one side of the square came two sticks with length 1.
Example 2:
Input: [3,3,3,3,4]
Output: false
Explanation: You cannot find a way to form a square with all the matchsticks.
Note:
The length sum of the given matchsticks is in the range of 0 to 10^9.
The length of the given matchstick array will not exceed 15.
|
class Solution:
def makesquare(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
def checkv(t,c,nums,used):
if t==0:
return True
if t<0:
return False
while c<len(nums) and used[c]:
c=c+1
if c>=len(nums):
return False
if checkv(t-nums[c],c+1,nums,used):
used[c]=True
return True
if checkv(t,c+1,nums,used):
return True
return False
edgel=sum(nums)
if edgel%4!=0 or edgel==0:
return False
edgel=edgel/4
nums.sort(key=lambda x:-x)
n=len(nums)
used=[False]*n
for p in range(3):
t=edgel
if not checkv(t,0,nums,used):
return False
return True
|
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.
Your input will be several matchsticks the girl has, represented with their stick length. Your output will either be true or false, to represent whether you could make one square using all the matchsticks the little match girl has.
Example 1:
Input: [1,1,2,2,2]
Output: true
Explanation: You can form a square with length 2, one side of the square came two sticks with length 1.
Example 2:
Input: [3,3,3,3,4]
Output: false
Explanation: You cannot find a way to form a square with all the matchsticks.
Note:
The length sum of the given matchsticks is in the range of 0 to 10^9.
The length of the given matchstick array will not exceed 15.
|
class Solution:
def makesquare(self, nums):
s = sum(nums)
if not s % 4 == 0:
return False
l = s // 4
from collections import Counter
self.c = Counter(nums)
for _ in range(4):
n = self.f(0, sorted(self.c.elements(),reverse=True), l, ())
if not n:
return False
self.c.subtract(n)
return True
def f(self, index, keys, sum, nums):
if sum == 0:
return nums
if sum < 0 or index >= len(keys):
return None
return self.f(index + 1, keys, sum - keys[index], (*nums, keys[index])) \
or self.f(index + 1, keys, sum, nums)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
mod = int(1e9)+7
return ( ( self.getMax(horizontalCuts, h) % mod ) * ( self.getMax(verticalCuts, w) % mod ) ) % mod
def getMax(self, cuts, size):
if len(cuts) == 1:
return max(cuts[0], size - cuts[0])
cuts.sort()
max_cut_size = max(cuts[0], size - cuts[-1])
for index in range(1, len(cuts)):
if cuts[index] - cuts[index - 1] > max_cut_size:
max_cut_size = cuts[index] - cuts[index - 1]
return max_cut_size
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
h_max = self.get_max_interval(w, sorted(verticalCuts))
w_max = self.get_max_interval(h, sorted(horizontalCuts))
return (h_max*w_max) % (10**9 + 7)
def get_max_interval(self, length, cuts):
current = 0
max_cut = 0
for cut in cuts:
max_cut = max(max_cut, cut - current)
current = cut
max_cut = max(max_cut, length - current)
return max_cut
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.sort()
verticalCuts.sort()
def getGap(arr, n):
prev=0
i=0
res=0
while i<len(arr):
res=max(arr[i]-prev, res)
prev=arr[i]
i+=1
return max(n-prev, res)
return (getGap(horizontalCuts, h)*getGap(verticalCuts, w))%(10**9 + 7)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.