description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: def check(position, d, m): last = position[0] balls = m - 1 i = 1 while balls and i < len(position): if position[i] - last < d: i += 1 else: last = position[i] balls -= 1 return balls == 0 position.sort() hi = position[-1] lo = 1 while lo < hi: mi = (lo + hi + 1) // 2 if check(position, mi, m): lo = mi else: hi = mi - 1 return lo
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, positions: List[int], m: int) -> int: positions.sort() def possible(force): placed = 0 pos = -math.inf for p in positions: if p - pos >= force: placed += 1 pos = p return placed >= m lo, hi = 0, positions[-1] while lo <= hi: mid = (lo + hi) // 2 if possible(mid): lo = mid + 1 else: hi = mid - 1 return hi
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR RETURN NUMBER VAR NUMBER RETURN NUMBER
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: def ispossible(A, force, n): n -= 1 last = A[0] for i in range(1, len(A)): if n > 0: if A[i] - last >= force: n -= 1 last = A[i] else: break if n == 0: return True return False A = position A.sort() l = 1 r = A[-1] ans = 1 while r >= l: mid = (r + l) // 2 if ispossible(A, mid, m): ans = mid l = mid + 1 else: r = mid - 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() w = (position[-1] - position[0]) // (m - 1) def count(w, m): idx = 0 m -= 1 for i in range(1, len(position)): if position[i] - position[idx] >= w: idx = i m -= 1 if m == 0: break return m == 0 if count(w, m): return w else: l, r = 0, w while l < r: mid = r - (r - l) // 2 if count(mid, m): l = mid else: r = mid - 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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 = -1 if m == 2: return position[-1] - position[0] def count(spacing): count = 1 prev_pos = position[0] for pos in position: if pos - prev_pos >= spacing: count += 1 prev_pos = pos return count left, right = 1, math.ceil(position[-1] / (m - 1)) while left <= right: mid = (left + right) // 2 if count(mid) >= m: res = mid left = mid + 1 else: right = mid - 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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() left = 1 right = max(position) while right - left > 1: mid = (left + right) // 2 if self.ok(position, m, mid): left = mid else: right = mid if self.ok(position, m, right): return right elif self.ok(position, m, left): return left def ok(self, position, m, force): n = len(position) stack = [position[0]] i = 0 while i < n: if position[i] - stack[-1] >= force: stack.append(position[i]) i += 1 return len(stack) >= m
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN VAR IF FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position = sorted(position) l = 1 r = position[-1] - position[0] def check(diff, m): ctr = 1 prev = 0 for i in range(1, len(position)): if ctr == m: return True if position[i] - position[prev] >= diff: ctr += 1 prev = i if ctr == m: return True return False val = -(10**10) while l <= r: mid = (l + r) // 2 if check(mid, m): val = max(val, mid) l = mid + 1 else: r = mid - 1 return val
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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 elif val - prev_val >= force: balls_placed += 1 prev_val = val if balls_placed > m - 1: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR IF VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, p: List[int], m: int) -> int: def isvalid(f: int): buckets, pos = 1, p[0] for x in p: if f <= x - pos: buckets += 1 if buckets == m: return True pos = x return False p.sort() lb, ub = 1, (p[-1] - p[0]) // (m - 1) + 1 while 1 < ub - lb: f = (lb + ub) // 2 if isvalid(f): lb = f else: ub = f return lb
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER WHILE NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() def place(d): print(d) ans = 1 pos = position[0] + d j = 1 while pos <= position[-1]: while j < len(position) and position[j] < pos: j += 1 if j == len(position): break ans += 1 pos = position[j] + d j += 1 print("ans") print(ans) return ans l = 0 r = position[-1] - position[0] while l <= r: mid = (l + r) // 2 if place(mid) >= m: l = mid + 1 else: r = mid - 1 return l - 1
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: def validPlacement(spacing): placed, curPosition = 1, position[0] for p in position: if p - curPosition >= spacing: placed += 1 curPosition = p return placed >= m position.sort() lo, hi = 0, position[-1] - position[0] while lo < hi: mid = hi - (hi - lo) // 2 if validPlacement(mid): lo = mid else: hi = mid - 1 return lo
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() delta = [] for i in range(len(position) - 1): delta.append(position[i + 1] - position[i]) lo = min(delta) hi = sum(delta) m -= 1 def isPossible(minForce): total = 0 count = 0 for d in delta: total += d if total >= minForce: count += 1 total = 0 if count == m: return True return False while lo <= hi: mid = (lo + hi) // 2 if isPossible(mid): lo = mid + 1 else: hi = mid - 1 return lo - 1
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
def check(x, pos, m): c = 0 prev = -1000000000000000 for i in range(len(pos)): if abs(pos[i] - prev) >= x: c += 1 prev = pos[i] return c >= m class Solution: def maxDistance(self, pos: List[int], m: int) -> int: pos.sort() n = len(pos) l = 1 r = pos[-1] while l <= r: mid = l + (r - l) // 2 if check(mid, pos, m): l = mid + 1 else: r = mid - 1 if check(l, pos, m): return l else: return r
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == 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
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: def feasible(d, position): balls = [position[0]] placed = 1 i = 1 while i <= len(position) - 1: if position[i] - balls[-1] >= d: balls.append(position[i]) placed += 1 if placed == m: return True i += 1 return False position.sort() left, right = 1, max(position) while left < right: mid = right - (right - left) // 2 if feasible(mid, position): left = mid else: right = mid - 1 return left
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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() distance = [(0) for _ in range(len(position) - 1)] for i in range(len(position) - 1): diff = position[i + 1] - position[i] distance[i] = diff left = min(distance) right = position[-1] - position[0] def check(diff, m): m -= 1 pre_dis = 0 for i in range(0, len(distance)): if distance[i] + pre_dis >= diff: m -= 1 if m <= 0: return True pre_dis = 0 else: pre_dis += distance[i] return False while left < right: mid = (left + right + 1) // 2 if check(mid, m) == True: left = mid else: right = mid - 1 return left
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR VAR VAR RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() def trial(n): prev, curr, ct = 0, 1, 1 while True: if curr >= len(position): return False if position[curr] - position[prev] >= n: prev = curr ct += 1 if ct == m: return True curr += 1 l, r = 1, position[-1] - position[0] + 1 ret = 0 while l < r: mid = (l + r) // 2 ans = trial(mid) if ans: l = mid + 1 else: r = mid return r - 1
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, pos: [int], m: int) -> int: pos.sort() N = len(pos) high = (pos[-1] - pos[0]) // (m - 1) low = 1 def check(dist): cnt = 1 loc = pos[0] for i in range(1, N): if pos[i] - loc >= dist: cnt += 1 loc = pos[i] if cnt == m: return True return False while high != low: mid = (high + low + 1) // 2 if check(mid): low = mid else: high = mid - 1 return high
CLASS_DEF FUNC_DEF LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() n = len(position) high = position[-1] - position[0] low = 1 while high != low: mid = (high + low + 1) // 2 balls = 1 start = position[0] for i in range(1, n): if position[i] - start >= mid: balls += 1 start = position[i] if balls >= m: low = mid else: high = mid - 1 return low
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() def valid(dis): count = 1 prev = position[0] for n in position: if n - prev >= dis: count += 1 prev = n if count == m: return True return False l, r = 0, position[-1] ret = 0 while l < r: mid = l + (r - l) // 2 if valid(mid): ret = max(ret, mid) l = mid + 1 else: r = mid return ret
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() l, r = 1, position[-1] - position[0] + 1 def isInvalid(val): ball = 1 previous = position[0] for p in position: if p - previous < val: continue ball += 1 previous = p return ball < m while l < r: mid = l + (r - l) // 2 if isInvalid(mid): r = mid else: l = mid + 1 return l - 1
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() L = len(position) left = 1 right = position[-1] - position[0] def can_place(f): n_ball = m - 1 cum_dis = 0 p = 1 while n_ball > 0 and p < L: cum_dis += position[p] - position[p - 1] if cum_dis >= f: n_ball -= 1 cum_dis = 0 p += 1 if n_ball == 0: return True else: return False while left < right: mid = (left + right) // 2 if can_place(mid): left = mid else: right = mid - 1 if right - left == 1: if can_place(right): return right else: return left return left
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN VAR RETURN VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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) hi = 10**9 - 1 li = 0 while li <= 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
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR RETURN VAR RETURN VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: n = len(position) position.sort() def helper(d): ans, curr = 1, position[0] for i in range(1, n): if position[i] - curr >= d: ans += 1 curr = position[i] return ans left, right = 0, position[-1] - position[0] while left <= right: mid = left + (right - left) // 2 if helper(mid) < m: right = mid - 1 else: left = mid + 1 return left - 1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: n = len(position) position.sort() l, r = 1, position[-1] - position[0] res = r while l <= r: mid = (l + r) // 2 prev, i, cnt = position[0], 1, m - 1 while i < n and cnt > 0: if position[i] - prev >= mid: cnt -= 1 prev = position[i] i += 1 if cnt > 0: r = mid - 1 else: res = mid l = mid + 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def put(self, position, d, m): n = 1 curr = position[0] for i in range(1, len(position)): if position[i] - curr >= d: n += 1 curr = position[i] if n >= m: return 1 return 0 def maxDistance(self, position: List[int], m: int) -> int: position.sort() low, high = 0, position[-1] while low < high: mid = (high + low + 1) // 2 comp = self.put(position, mid, m) if comp > 0: low = mid else: high = mid - 1 return low
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position, m): position = sorted(position) start, end = 1, position[-1] - position[0] while start + 1 < end: mid = start + (end - start) // 2 if self.check(position, m, mid): start = mid else: end = mid if self.check(position, m, end): return end if self.check(position, m, start): return start def check(self, position, m, delta): n = len(position) i = 1 j = 1 last = position[0] while i <= m - 1 and j <= n - 1: if position[j] >= last + delta: last = position[j] i += 1 j += 1 return i == m
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN VAR IF FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() l, r = 1, position[-1] - position[0] + 1 while l != r: mid = (l + r) // 2 + (l + r) % 2 x = self.balls(position, mid) if x < m: r = mid - 1 elif x >= m: l = mid return l def balls(self, position, d): ans = 1 cur = position[0] for i in range(1, len(position)): if position[i] - cur >= d: cur = position[i] ans += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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 find_distance(mid_pos): count = 1 pre = position[0] for i in range(1, len(position)): distance = position[i] - pre if distance >= mid_pos: count += 1 pre = position[i] return count position = sorted(position) lo = 0 hi = position[-1] while lo <= hi: mid = lo + (hi - lo) // 2 if find_distance(mid) >= m: lo = mid + 1 else: hi = mid - 1 return hi
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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: int) -> bool: curr = position[0] count = 1 for i in range(len(position)): if abs(position[i] - curr) >= dist: curr = position[i] count += 1 if i == len(position) - 1: if count >= m: return True else: return False lo = 1 hi = max(position) - min(position) while lo < hi: mid = lo + (hi - lo + 1) // 2 if check(mid): lo = mid else: hi = mid - 1 return lo
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def possible(self, position, dist, m): prev = float("-inf") cnt = 0 for x in position: if x < prev + dist: continue cnt += 1 prev = x return cnt >= m def maxDistance(self, position: List[int], m: int) -> int: position.sort() low, high = 0, max(position) while low + 1 < high: mid = (low + high) // 2 if self.possible(position, mid, m): low = mid else: high = mid if self.possible(position, high, m): return high return low
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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 else: r = mid - 1 return pos
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() l = 1 r = (position[-1] - position[0]) // (m - 1) while l <= r: mid = (l + r) // 2 placed = 1 distance = 0 start = position[0] for i in range(1, len(position)): distance = position[i] - start if distance >= mid: placed += 1 distance = 0 start = position[i] if placed >= m: answer = mid l = mid + 1 else: r = mid - 1 return answer
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def feasible(self, d, m): last_pos = self.position[0] i = 1 num_placed = 1 while num_placed < m and i < self.plen: if self.position[i] - last_pos >= d: last_pos = self.position[i] num_placed += 1 i += 1 return num_placed == m def maxDistance(self, position: List[int], m: int) -> int: self.position = position self.plen = len(self.position) self.position.sort() left = 1 right = self.position[-1] - self.position[0] + 1 while left + 1 < right: mid = (left + right) // 2 if self.feasible(mid, m): left = mid else: right = mid return left
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, a: List[int], m: int) -> int: def check(d): x, k = 0, m for i in range(len(a)): if a[i] >= x: x, k = a[i] + d, k - 1 if k == 0: break return k == 0 a.sort() l, r = 1, (max(a) - min(a)) // (m - 1) while l < r: d = r - (r - l) // 2 if check(d): l = d else: r = d - 1 return r if check(r) else l
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: n = len(position) position.sort() def small_enough(mid): count = prev = 0 for i, p in enumerate(position): if i == 0: count += 1 elif p - prev >= mid: count += 1 else: continue prev = p return count >= m l, h = 1, position[-1] - position[0] while l < h: mid = (l + h + 1) // 2 if small_enough(mid): l = mid else: h = mid - 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: n = len(position) def check(d): i, b = 0, 1 for j in range(1, n): if position[j] - position[i] >= d: b += 1 i = j return b >= m position = sorted(position) lo, hi = 1, position[-1] while lo < hi: mid = (lo + hi) // 2 if check(mid): lo = mid + 1 else: hi = mid return lo - 1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
def isPossible(arr, n, C, mid): magnet = 1 currPosition = arr[0] for i in range(1, n): if arr[i] - currPosition >= mid: magnet += 1 currPosition = arr[i] if magnet == C: return True return False def binarySearch(n, C, arr): arr.sort(reverse=False) lo = 0 hi = arr[n - 1] ans = 0 while lo <= hi: mid = int((lo + hi) / 2) if isPossible(arr, n, C, mid) == False: hi = mid - 1 else: ans = max(ans, mid) lo = mid + 1 return ans class Solution: def maxDistance(self, arr: List[int], k: int) -> int: n = len(arr) return binarySearch(n, k, arr)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() def helper(position, x, m): prev = position[0] m -= 1 for i in range(1, len(position)): if position[i] - prev >= x: prev = position[i] m -= 1 if m == 0: break if m == 0: return x else: return -1 l = 1 r = position[-1] - position[0] + 1 ans = 0 while l < r: mid = (l + r) // 2 realMinimumForce = helper(position, mid, m) print((mid, realMinimumForce)) if realMinimumForce == -1: r = mid else: l = mid + 1 ans = max(ans, realMinimumForce) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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 if cnt < m: r = mid else: l = mid + 1 return l - 1
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: def check_works(position, m, dist): balls_to_place = m - 1 last_ball_pos = 0 for ind in range(last_ball_pos, len(position)): if position[ind] - position[last_ball_pos] >= dist: balls_to_place -= 1 last_ball_pos = ind if balls_to_place == 0: break if balls_to_place == 0: return 1 else: return 0 position.sort() lb = 1 rb = 1000000000 while lb != rb - 1: ret = check_works(position, m, int((lb + rb) / 2)) if ret == 0: rb = int((lb + rb) / 2) else: lb = int((lb + rb) / 2) print(lb, rb) return lb
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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) maxVal = position[-1] - position[0] answer = 0 low, high = 1, maxVal def isValid(mid): pos = position[0] elements = 1 for i in range(1, len(position)): if position[i] - pos >= mid: pos = position[i] elements += 1 if elements == m: return True return False while low <= high: mid = (low + high) // 2 if isValid(mid): answer = max(answer, mid) low = mid + 1 else: high = mid - 1 return answer
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() n = len(position) res = -1 left = position[0] right = position[n - 1] while left < right: mid = (left + right) // 2 if self.isFeasible(position, mid, m): res = max(res, mid) left = mid + 1 else: right = mid if res == -1: left = 0 right = position[0] while left < right: mid = (left + right) // 2 if self.isFeasible(position, mid, m): res = max(res, mid) left = mid + 1 else: right = mid return res def isFeasible(self, arr, mid, m): n = len(arr) pos = arr[0] count = 1 for i in range(1, n): if arr[i] - pos >= mid: pos = arr[i] count += 1 if count == m: return True return False
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() def ifvalid(diff, num_division): selected = 0 cur = position[0] for i, f in enumerate(position): if selected >= num_division: return True if f - cur >= diff: selected += 1 cur = position[i] elif i == len(position) - 1: return False return selected >= num_division def search(l, r): while l < r: mid = l + (r - l) // 2 if not ifvalid(mid, m - 1): r = mid else: l = mid + 1 return l - 1 max_diff = (position[-1] - position[0]) // (m - 1) + 1 return search(1, max_diff + 1)
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN VAR VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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: mid = left + (right - left) // 2 if valid(mid): left = mid else: right = mid return left
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: def check(arr, n, m, mid): magnet = 1 pos = arr[0] for i in range(1, n): if arr[i] - pos >= mid: magnet += 1 pos = arr[i] if magnet == m: return 1 return 0 position.sort() l = 0 n = len(position) r = position[n - 1] res = 0 while l <= r: mid = (l + r) // 2 if not check(position, n, m, mid): r = mid - 1 else: res = max(res, mid) l = mid + 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() min_p, max_p = position[0], position[-1] def enough(x): prev, cnt = min_p, 1 for i, p in enumerate(position): if p - prev >= x: prev = p cnt += 1 if cnt >= m: return True return False l, r = 1, max_p - min_p while l < r: mid = (l + r + 1) // 2 if enough(mid): l = mid else: r = mid - 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: def check(position, val, m): balls = 1 prev = position[0] for i in range(1, len(position)): if position[i] - prev >= val: prev = position[i] balls += 1 if balls >= m: return True return False position.sort() lo, hi = 0, position[-1] - position[0] while lo < hi: mid = (hi + lo + 1) // 2 if check(position, mid, m): lo = mid else: hi = mid - 1 return lo
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: def check(position, m, f): prev = 0 balls = 1 index = 1 while index < len(position) and balls <= m: if position[index] - position[prev] >= f: prev = index index += 1 balls += 1 else: index += 1 if balls >= m: return True else: return False position = sorted(position) start = 1 end = max(position) while start < end: mid = (start + end) // 2 if check(position, m, mid): start = mid + 1 else: end = mid return start - 1
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, A, m: int) -> int: A.sort() def test(mid): cnt = m - 1 cur = A[0] for i in A[1:]: if i - cur >= mid: cnt -= 1 cur = i else: continue if cnt == 0: return True return False mi = 1 ma = (A[-1] - A[0]) // (m - 1) while mi <= ma: mid = (mi + ma) // 2 if test(mid): mi = mid + 1 else: ma = mid - 1 return ma
CLASS_DEF FUNC_DEF VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() def realizable(value): last = position[0] placed = 1 for k in range(1, len(position)): if position[k] - last >= value: placed += 1 last = position[k] return placed >= m min_dist, max_dist = position[1] - position[0], position[-1] - position[0] for k in range(1, len(position)): min_dist = min(min_dist, position[k] - position[k - 1]) left, right = min_dist, max_dist + 1 print(left, right) while left < right: mid = left + (right - left) // 2 if not realizable(mid): right = mid else: left = mid + 1 return left - 1
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() mid = max(position) // 2 gap = max(1, mid // 2) t = 0 best = 0 while True: c = 1 last = position[0] for i in range(1, len(position)): if position[i] - mid >= last: c += 1 last = position[i] if c >= m: best = max(best, mid) mid = mid + gap else: mid = mid - gap gap = gap // 2 gap = max(1, gap) t += 1 if t == 50: break return best
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER IF VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: 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() maxReally = (position[-1] - position[0]) / (m - 1) 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: minForce = middleForce + 1 result = middleForce else: maxForce = middleForce - 1 return int(result)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() posSorted = position low = 1 high = posSorted[-1] mid = (low + high) // 2 while low < high: if self.validForce(posSorted, m, mid): low = mid else: high = mid - 1 mid = high - (high - low) // 2 return low def validForce(self, positions, m, mid): used = 0 lastpos = 0 for i, pos in enumerate(positions): if i == 0: used += 1 continue if pos - positions[lastpos] >= mid: lastpos = i used += 1 if used >= m: return True return False
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: n = len(position) position = sorted(position) def count(d, position): m_num, cur = 1, position[0] for p in position: if p - cur >= d: m_num += 1 cur = p return m_num l = 0 r = position[-1] - position[0] while l < r: mid = l + (r - l) // 2 m_num = count(mid + 1, position) if m_num < m: r = mid else: l = mid + 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: def canFindGaps(d): left = 0 found = i = 1 while i < len(position) and found < m: if position[i] - position[left] >= d: found += 1 left = i i += 1 return found == m position.sort() max_diff = position[-1] - position[0] if m == 2: return max_diff left, right = 1, max_diff // (m - 1) while left < right: mid = (left + right) // 2 if canFindGaps(mid): left = mid + 1 else: right = mid - 1 if right < left: return right return left if canFindGaps(left) else left - 1
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() N = len(position) def place(d): pre = position[0] idx = 1 for i in range(m - 1): while idx < N: if position[idx] >= pre + d: break else: idx += 1 if idx == N: return False else: pre = position[idx] return True l = 1 r = position[-1] - position[0] while l < r: mid = (l + r + 1) // 2 if place(mid): l = mid else: r = mid - 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position = sorted(position) highest_f = int((position[-1] - position[0]) / (m - 1)) def helper(position, m, high_f, low_f, curr_f): selected_pos = [position[0]] for i in position: if i - selected_pos[-1] >= curr_f: selected_pos.append(i) if high_f > low_f + 1: if len(selected_pos) < m: new_curr_f = int((low_f + curr_f) / 2) return helper(position, m, curr_f, low_f, new_curr_f) else: new_curr_f = int((curr_f + high_f) / 2) return helper(position, m, high_f, curr_f, new_curr_f) elif len(selected_pos) < m: return low_f else: return curr_f return int(helper(position, m, highest_f, 1, highest_f))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR LIST VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() start, end = 0, position[-1] while start + 1 < end: mid = (start + end) // 2 if self.isValid(position, mid) < m: end = mid else: start = mid if self.isValid(position, end) < m: return start else: return end def isValid(self, position, dist): cnt = 1 i = 1 current = position[0] + dist while i < len(position): if position[i] >= current: cnt += 1 current = position[i] + dist i += 1 return cnt
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: N = len(position) A = position A.sort() lo, hi = 1, A[-1] - A[0] while lo <= hi: mid = (lo + hi + 1) // 2 prev = A[0] left = m - 1 for i in range(1, N): if A[i] - prev >= mid: prev = A[i] left -= 1 if left == 0: break if left == 0: answer = mid lo = mid + 1 else: hi = mid - 1 return answer
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() l, r = 1, position[-1] - position[0] + 2 ans = 0 while l < r: mid = (l + r + 1) // 2 cnt, i, j = 1, 1, 0 while i < len(position): if position[i] - position[j] >= mid: cnt += 1 j = i i += 1 if cnt >= m: l = mid else: r = mid - 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: def getBallCount(position, force): res, prev = 1, 0 for i in range(1, len(position)): if position[i] - position[prev] >= force: res += 1 prev = i return res position.sort() if m == 2: return position[-1] - position[0] l, r, res = 0, position[-1] + 1, 0 while l < r: mid = l + (r - l) // 2 if getBallCount(position, mid) >= m: l = mid + 1 res = mid else: r = mid return res
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() diff = position[-1] - position[0] right = diff // (m - 1) left = 1 def condition(num): k = m - 1 prev = position[0] for n in position[1:]: if n - prev >= num: k -= 1 prev = n if k == 0: return True return False while left < right: mid = left + (right - left) // 2 if condition(mid): left = mid + 1 else: right = mid if condition(left): return left else: return left - 1 return left
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR RETURN VAR RETURN BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 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 can_distribute(baskets, ball_count, min_dist): last = [] for basket in baskets: if not last or last[-1] + min_dist <= basket: last.append(basket) if len(last) == ball_count: return True return False def first(a, b, p): if a >= b: return a elif p(a): return a elif not p(b - 1): return b else: m = (a + b) // 2 if p(m - 1): return first(a, m, p) elif not p(m): return first(m, b, p) else: return m def last(a, b, p): return first(a, b, lambda x: not p(x)) - 1 def max_dist(baskets, ball_count): baskets = sorted(baskets) theoretical_max_dist = (baskets[-1] - baskets[0]) // (ball_count - 1) return last( 1, theoretical_max_dist + 1, lambda dist: can_distribute(baskets, ball_count, dist), ) class Solution: def maxDistance(self, position: List[int], m: int) -> int: return max_dist(position, m)
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN VAR IF FUNC_CALL VAR VAR RETURN VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() l, r = 1, position[-1] - position[0] while l < r: mid = l + (r - l + 1) // 2 placefind = 1 preIndex = 0 for i in range(1, len(position)): if position[i] - position[preIndex] >= mid: placefind += 1 preIndex = i if placefind >= m: l = mid else: r = mid - 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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 dist_valid(interval, k): count, k_count = 0, 0 for i in interval: count += i if count >= k: count = 0 k_count += 1 if k_count >= m - 1: return True return False interval = [] sort_pos = sorted(position) for i in range(len(sort_pos) - 1): interval.append(sort_pos[i + 1] - sort_pos[i]) left = 1 right = (max(sort_pos) - min(sort_pos)) // (m - 1) while left < right: mid = right - (right - left) // 2 if dist_valid(interval, mid): left = mid else: right = mid - 1 return left
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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])
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 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(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] 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
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() maxrange = position[-1] - position[0] l, r = 1, maxrange while l <= r: mid = l + (r - l) // 2 cnt = self.count(position, mid) if cnt >= m: l = mid + 1 else: r = mid - 1 return r def count(self, poss: list, dist: int) -> int: res = 1 last = poss[0] for i in range(1, len(poss)): if poss[i] - last >= dist: res += 1 last = poss[i] return res
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() if m == 2: return position[-1] - position[0] print(position) left, right = 1, position[-1] - position[0] def valid(mid): count = 1 last = position[0] for i in range(1, len(position)): if position[i] - last >= mid: last = position[i] count += 1 return count >= m while left < right: mid = (right - left + 1) // 2 + left if valid(mid): left = mid else: right = mid - 1 return left
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() def check(d): i = 0 j = 1 for _ in range(m - 1): while j < len(position) and position[j] - position[i] < d: j += 1 if j >= len(position): return False i = j j = j + 1 return True a = 1 b = position[-1] - position[0] ans = 0 while a <= b: c = (a + b) // 2 if check(c): ans = c a = c + 1 else: b = c - 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() def f(mid, position, m): f = position[0] m = m - 1 for j in range(1, len(position)): if position[j] - f >= mid: f = position[j] m = m - 1 if m < 0: break if m <= 0: return True return False h = (position[-1] - position[0]) // (m - 1) + 1 l = 0 while h >= l: mid = (h + l) // 2 print(mid) if f(mid, position, m): l = mid + 1 else: h = mid - 1 return l - 1
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() mx = position[-1] lo, hi = 1, mx def judge(x): pre, cnt = -mx, 0 for p in position: if p - pre >= x: pre = p cnt += 1 if cnt >= m: return True return False while lo <= hi: mid = (lo + hi) // 2 if judge(mid): lo = mid + 1 else: hi = mid - 1 return hi
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position, m): position.sort() N = len(position) l = math.inf for i in range(1, N): l = min(l, position[i] - position[i - 1]) r = position[-1] - position[0] + 1 def check(k): last = position[0] res = 0 for i in range(1, N): if position[i] - last >= k: res += 1 last = position[i] return res while l < r: k = (l + r) // 2 if check(k) <= m - 2: r = k else: l = k + 1 return r - 1
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: def feasible(min_req_dist, b): i = 0 prev_pos = None while i < len(position) and b > 0: if prev_pos == None or position[i] - prev_pos >= min_req_dist: b -= 1 prev_pos = position[i] i += 1 return b == 0 position.sort() n = len(position) low = 1 high = position[-1] while low < high: mid = (low + high) // 2 + 1 if feasible(mid, m): low = mid else: high = mid - 1 return low
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NONE WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NONE BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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 return max_distance
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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() ans = top = (position[-1] - position[0]) // (m - 1) down = 0 while ans != down: if self._test(position, ans, m - 1): down = ans else: top = ans ans = (top + down) // 2 return down def _test(self, position, force, m): i = 0 while i < len(position) and m > 0: if len(position) - 1 - i < m: return False total = 0 while total < force: i += 1 if i >= len(position): break total += position[i] - position[i - 1] if total >= force: m -= 1 if m == 0: return True return False
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: def c(p, m, d): b = m - 1 l = len(p) acc = 0 for i in range(1, l): acc += p[i] - p[i - 1] if acc >= d: acc = 0 b -= 1 if b == 0: return True return False low = 1 high = max(position) mid = 0 position.sort() while low <= high: mid = (low + high) // 2 if c(position, m, mid): low = mid + 1 else: high = mid - 1 return high
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, pos: List[int], m: int) -> int: pos.sort() def isfeasible(dist): cur = pos[0] n = 1 for i in range(len(pos)): if pos[i] >= cur + dist: cur = pos[i] n += 1 return n >= m ng = pos[-1] - pos[0] + 1 ok = 1 while abs(ok - ng) > 1: mid = ok + (ng - ok) // 2 if isfeasible(mid): ok = mid else: ng = mid return ok
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR RETURN VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() lo, hi = 1, (position[-1] - position[0]) // (m - 1) + 1 res = 0 def possible(gap): balls = m prev = None for i in range(len(position)): if not prev: prev = position[i] balls -= 1 elif abs(prev - position[i]) < gap: continue else: prev = position[i] balls -= 1 if balls <= 0: return True return False while lo <= hi: mid = (lo + hi) // 2 if possible(mid): res = mid lo = mid + 1 else: hi = mid - 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: def isPossible(lst, m, force): pos = lst[0] for i in range(1, len(lst)): if lst[i] - pos >= force: m -= 1 pos = lst[i] return m < 2 l = 0 r = 1000000000 position.sort() while l < r: mid = (l + r >> 1) + 1 if isPossible(position, m, mid): l = mid else: r = mid - 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position = sorted(position) minp, maxp = position[0], position[-1] lo, hi = 1, (maxp - minp) // (m - 1) + 1 ans = lo def distributable(n): pos = position.copy() balls = m while pos and balls: balls -= 1 np = pos[-1] - n while pos and pos[-1] > np: pos.pop() return not balls while lo < hi: mid = lo + (hi - lo) // 2 if distributable(mid): ans = mid lo = mid + 1 else: hi = mid return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[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
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
def count(position, m, n, d): ans, curr = 1, position[0] for i in range(1, n): if position[i] - curr >= d: ans += 1 curr = position[i] if ans == m: return True return False class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() n = len(position) l, r = 1, 10**9 + 7 res = -1 while l < r: mid = (l + r) // 2 if count(position, m, n, mid): res = max(res, mid) l = mid + 1 else: r = mid return res
FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() @lru_cache(None) def helper(gap): prev = 0 cnt = 1 while cnt < m: idx = prev + 1 while idx < len(position) and position[idx] - position[prev] < gap: idx += 1 if idx >= len(position): break prev = idx cnt += 1 return cnt == m lb, ub = 1, position[-1] - position[0] + 1 while lb < ub: mid = lb + (ub - lb) // 2 if helper(mid): if not helper(mid + 1): return mid else: lb = mid + 1 else: ub = mid return lb
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR NONE ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 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
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() n = len(position) def check(force): k = m - 1 pre = 0 i = 1 while i < n: while i < n and position[i] - position[pre] < force: i += 1 if i < n: k -= 1 if k == 0: return True pre = i i += 1 return False lo, hi = 1, position[-1] while lo < hi: mi = lo + (hi - lo) // 2 if check(mi): lo = mi + 1 else: hi = mi return lo - 1
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() def is_valid(guess): prev = position[0] balls = 1 for p in position[1:]: if p - prev >= guess: prev = p balls += 1 if balls == m: return True return False l, r = 1, (position[-1] - position[0]) // (m - 1) while l < r: guess = l + r + 1 >> 1 if is_valid(guess): l = guess else: r = guess - 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def distribute(self, position, m, dist): prev = position[0] m -= 1 for i in range(1, len(position)): if position[i] - prev >= dist: prev = position[i] m -= 1 if m == 0: return True return False def maxDistance(self, position: List[int], m: int) -> int: position.sort() low, high = 0, position[-1] - position[0] res = 0 while low <= high: mid = (high - low) // 2 + low if self.distribute(position, m, mid): res = mid low = mid + 1 else: high = mid - 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() high = int((position[-1] - position[0]) / (m - 1)) low = 0 while high - 1 > low: mid = int((high + low) / 2) if self.helper(position, mid, m): low = mid else: 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 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
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum. Rick stated that magnetic force between two different balls at positions x and y is |x - y|. Given the integer array position and the integer m. Return the required force.   Example 1: Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3. Example 2: Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.   Constraints: n == position.length 2 <= n <= 10^5 1 <= position[i] <= 10^9 All integers in position are distinct. 2 <= m <= position.length
class Solution: def maxDistance(self, position: List[int], m: int) -> int: position.sort() l = 0 r = (position[-1] - 1) // (m - 1) * 2 while l < r: intrv = (l + r) // 2 c = 0 prev = float("-inf") for p in position: if p >= prev + intrv: c += 1 prev = p if c >= m: best = intrv l = intrv + 1 else: r = intrv return best
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR