description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given an array of n distinct elements. Find the minimum number of swaps required to sort the array in strictly increasing order.
Example 1:
Input:
nums = {2, 8, 5, 4}
Output:
1
Explaination:
swap 8 with 4.
Example 2:
Input:
nums = {10, 19, 6, 3, 5}
Output:
2
Explaination:
swap 10 with 3 and swap 19 with 5.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSwaps() which takes the nums as input parameter and returns an integer denoting the minimum number of swaps required to sort the array. If the array is already sorted, return 0.
Expected Time Complexity: O(nlogn)
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ nums_{i} ≤ 10^{6} | class Solution:
def minSwaps(self, nums):
def swap(i, idx, l1):
temp = l1[i]
l1[i] = l1[idx]
l1[idx] = temp
l = []
for i in range(len(nums)):
l.append([nums[i], i])
l1 = sorted(l, key=lambda x: x[0])
c = 0
i = 0
while i < len(nums):
p = l1[i]
v = p[0]
idx = p[1]
if i != idx:
c = c + 1
swap(i, idx, l1)
i = i - 1
i = i + 1
return c | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given an array of n distinct elements. Find the minimum number of swaps required to sort the array in strictly increasing order.
Example 1:
Input:
nums = {2, 8, 5, 4}
Output:
1
Explaination:
swap 8 with 4.
Example 2:
Input:
nums = {10, 19, 6, 3, 5}
Output:
2
Explaination:
swap 10 with 3 and swap 19 with 5.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSwaps() which takes the nums as input parameter and returns an integer denoting the minimum number of swaps required to sort the array. If the array is already sorted, return 0.
Expected Time Complexity: O(nlogn)
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ nums_{i} ≤ 10^{6} | class Solution:
def minSwaps(self, nums):
n = len(nums)
ss = sorted(range(n), key=lambda x: nums[x])
r = 0
for i in range(n):
if ss[i] == i:
continue
rr = 0
while ss[i] != i:
k = ss[i]
ss[i], ss[k] = ss[k], k
rr += 1
r += rr
return r | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR |
Given an array of n distinct elements. Find the minimum number of swaps required to sort the array in strictly increasing order.
Example 1:
Input:
nums = {2, 8, 5, 4}
Output:
1
Explaination:
swap 8 with 4.
Example 2:
Input:
nums = {10, 19, 6, 3, 5}
Output:
2
Explaination:
swap 10 with 3 and swap 19 with 5.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSwaps() which takes the nums as input parameter and returns an integer denoting the minimum number of swaps required to sort the array. If the array is already sorted, return 0.
Expected Time Complexity: O(nlogn)
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ nums_{i} ≤ 10^{6} | class Solution:
def minSwaps(self, nums):
N = len(nums)
visited = [(False) for i in range(N)]
check = []
for i in range(N):
check.append([nums[i], i])
check.sort(key=lambda x: x[0])
for i in range(N):
if nums[i] == check[i][0]:
visited[i] = True
total = 0
for i in range(N):
if visited[i] == False:
visited[i] = True
index = check[i][1]
count = 0
while True:
if index == i:
break
visited[index] = True
index = check[index][1]
count += 1
total += count
return total | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR |
Given an array of n distinct elements. Find the minimum number of swaps required to sort the array in strictly increasing order.
Example 1:
Input:
nums = {2, 8, 5, 4}
Output:
1
Explaination:
swap 8 with 4.
Example 2:
Input:
nums = {10, 19, 6, 3, 5}
Output:
2
Explaination:
swap 10 with 3 and swap 19 with 5.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSwaps() which takes the nums as input parameter and returns an integer denoting the minimum number of swaps required to sort the array. If the array is already sorted, return 0.
Expected Time Complexity: O(nlogn)
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ nums_{i} ≤ 10^{6} | class Solution:
def minSwaps(self, nums):
graph = []
length = len(nums)
for i in range(length):
graph.append([nums[i], i])
graph.sort(key=lambda x: x[0])
visited = [(0) for i in range(length)]
swaps = 0
for i in range(length):
if visited[i] or graph[i][1] == i:
continue
cycle_size = 0
j = i
while not visited[j]:
visited[j] = 1
cycle_size += 1
j = graph[j][1]
swaps += cycle_size - 1
return swaps | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR |
Given an array of n distinct elements. Find the minimum number of swaps required to sort the array in strictly increasing order.
Example 1:
Input:
nums = {2, 8, 5, 4}
Output:
1
Explaination:
swap 8 with 4.
Example 2:
Input:
nums = {10, 19, 6, 3, 5}
Output:
2
Explaination:
swap 10 with 3 and swap 19 with 5.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSwaps() which takes the nums as input parameter and returns an integer denoting the minimum number of swaps required to sort the array. If the array is already sorted, return 0.
Expected Time Complexity: O(nlogn)
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ nums_{i} ≤ 10^{6} | class Solution:
def minSwaps(self, arr):
new = arr.copy()
new.sort()
dic = {}
count = 0
for i in range(n):
dic[new[i]] = i
for i in range(n):
while i != dic[arr[i]]:
temp = dic[arr[i]]
arr[i], arr[temp] = arr[temp], arr[i]
count += 1
return count | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | class Solution:
def kLargest(self, arr, n, k):
l = []
arr.sort()
arr.reverse()
for i in range(k):
l.append(arr[i])
return l | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | def heapify(arr, i, n):
index = i
lchild = 2 * index + 1
rchild = 2 * index + 2
if i >= n or lchild >= n:
return
if rchild < n and arr[index] < arr[rchild]:
index = rchild
if lchild < n and arr[index] < arr[lchild]:
index = lchild
if index != i:
arr[index], arr[i] = arr[i], arr[index]
heapify(arr, index, n)
def buildHeap(arr, n):
lp = n // 2 - 1
while lp > -1:
heapify(arr, lp, n)
lp -= 1
class Solution:
def kLargest(self, arr, n, k):
ans = []
if k <= n:
buildHeap(arr, n)
ans.append(arr[0])
k -= 1
while k > 0:
arr[0], arr[n - 1] = arr[n - 1], arr[0]
n -= 1
heapify(arr, 0, n)
ans.append(arr[0])
k -= 1
return ans | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | class Solution:
def partition(self, arr, s, e, p):
if s + 1 == e:
return s
j = s
v = arr[p]
arr[e - 1], arr[p] = arr[p], arr[e - 1]
for i in range(s, e):
if arr[i] > v:
arr[j], arr[i] = arr[i], arr[j]
j += 1
arr[j], arr[e - 1] = arr[e - 1], arr[j]
return j
def quickSort(self, arr, s, e):
if s + 1 >= e:
return
m = self.partition(arr, s, e, (s + e) // 2)
self.quickSort(arr, s, m)
self.quickSort(arr, m + 1, e)
def kLargest(self, arr, n, k):
s = 0
e = n
while True:
m = self.partition(arr, s, e, k)
if m == k:
break
if m > k:
e = m
else:
s = m + 1
self.quickSort(arr, 0, k)
return arr[:k] | CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | class Solution:
def kLargest(self, arr, n, k):
arr.sort()
x = arr[::-1]
y = []
for i in range(k):
y.append(x[i])
return y | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | class Solution:
def kLargest(self, arr, n, k):
a = []
arr.sort()
j = n - 1
for i in range(k):
a.append(arr[j])
j = j - 1
return a | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | class Solution:
def kLargest(self, arr, n, k):
arr.sort(reverse=True)
l = []
i = 0
while k != 0:
l.append(arr[i])
i += 1
k -= 1
return l | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | class Solution:
def kLargest(self, arr, n, k):
arr = sorted(arr, reverse=True)
b = []
n = len(arr)
for i in range(k):
b.append(arr[i])
return b | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | class Solution:
def kLargest(self, arr, n, k):
b = n - k
v = sorted(arr)
l = []
for i in range(n - 1, b - 1, -1):
l.append(v[i])
return l | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | class Solution:
def kLargest(self, arr, n, k):
a = []
arr.sort()
c = 0
for i in range(n - 1, -1, -1):
a.append(arr[i])
c += 1
if c == k:
break
return a | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | class Solution:
def kLargest(self, arr, n, k):
arr1 = []
v = n - k
arr.sort()
for i in range(n - 1, v - 1, -1):
arr1.append(arr[i])
return arr1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | class Solution:
def kLargest(self, arr, n, k):
sorted_arr = sorted(arr)
k_largestArr = sorted_arr[n - k :]
k_largestArr.reverse()
return k_largestArr | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | class Solution:
def kLargest(self, arr, n, k):
arr.sort()
a = []
for i in range(len(arr)):
if len(a) != k:
a.append(arr[n - 1 - i])
else:
return a | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | class Solution:
def kLargest(self, arr, n, k):
res = []
arr.sort()
arr.reverse()
i = 0
while i < k:
popped = arr.pop(0)
res.append(popped)
i += 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | class Solution:
def kLargest(self, arr, n, k):
arr.sort()
sze = len(arr) - 1
ans = []
while k != 0:
ans.append(arr[sze])
sze -= 1
k -= 1
return ans | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | class Solution:
def kLargest(self, arr, n, k):
def heapify(arr, n, i):
left = 2 * i + 1
right = 2 * i + 2
if left <= n - 1 and arr[left] > arr[i]:
largest = left
else:
largest = i
if right <= n - 1 and arr[right] > arr[largest]:
largest = right
if largest != i:
arr[largest], arr[i] = arr[i], arr[largest]
heapify(arr, n, largest)
def buildHeap(arr, n):
i = n // 2 - 1
while i >= 0:
heapify(arr, n, i)
i = i - 1
buildHeap(arr, n)
a = []
for i in range(k):
a.append(arr[0])
arr[0], arr[n - 1] = arr[n - 1], arr[0]
n = n - 1
heapify(arr, n, 0)
return a | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
Given an array Arr of N positive integers and an integer K, find K largest elements from the array. The output elements should be printed in decreasing order.
Example 1:
Input:
N = 5, K = 2
Arr[] = {12, 5, 787, 1, 23}
Output: 787 23
Explanation: 1st largest element in the
array is 787 and second largest is 23.
Example 2:
Input:
N = 7, K = 3
Arr[] = {1, 23, 12, 9, 30, 2, 50}
Output: 50 30 23
Explanation: 3 Largest element in the
array are 50, 30 and 23.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kLargest() which takes the array of integers arr, n and k as parameters and returns an array of integers denoting the answer. The array should be in decreasing order.
Expected Time Complexity: O(N + KlogK)
Expected Auxiliary Space: O(K+(N-K)*logK)
Constraints:
1 ≤ K ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6} | class Solution:
def kLargest(self, arr, n, k):
arr.sort()
li = []
for i in arr[n - k :]:
li.append(i)
return li[::-1] | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER |
There are $n$ points on a coordinate axis $OX$. The $i$-th point is located at the integer point $x_i$ and has a speed $v_i$. It is guaranteed that no two points occupy the same coordinate. All $n$ points move with the constant speed, the coordinate of the $i$-th point at the moment $t$ ($t$ can be non-integer) is calculated as $x_i + t \cdot v_i$.
Consider two points $i$ and $j$. Let $d(i, j)$ be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points $i$ and $j$ coincide at some moment, the value $d(i, j)$ will be $0$.
Your task is to calculate the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of points.
The second line of the input contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^8$), where $x_i$ is the initial coordinate of the $i$-th point. It is guaranteed that all $x_i$ are distinct.
The third line of the input contains $n$ integers $v_1, v_2, \dots, v_n$ ($-10^8 \le v_i \le 10^8$), where $v_i$ is the speed of the $i$-th point.
-----Output-----
Print one integer — the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Examples-----
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0 | def add(bit, x, v):
while x < len(bit):
bit[x] += v
x += x & -x
def query(bit, x):
ans = 0
while x > 0:
ans += bit[x]
x -= x & -x
return ans
def relabel(arr):
srt = sorted(set(arr))
mp = {v: k for k, v in enumerate(srt, 1)}
arr = [mp[a] for a in arr]
return arr
n = int(input())
x = list(map(int, input().split()))
v = relabel(list(map(int, input().split())))
arr = sorted(list(zip(x, v)))
ans = 0
bitSum = [0] * (n + 1)
bitCnt = [0] * (n + 1)
for x, v in arr:
ans += query(bitCnt, v) * x - query(bitSum, v)
add(bitSum, v, x)
add(bitCnt, v, 1)
print(ans) | FUNC_DEF WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are $n$ points on a coordinate axis $OX$. The $i$-th point is located at the integer point $x_i$ and has a speed $v_i$. It is guaranteed that no two points occupy the same coordinate. All $n$ points move with the constant speed, the coordinate of the $i$-th point at the moment $t$ ($t$ can be non-integer) is calculated as $x_i + t \cdot v_i$.
Consider two points $i$ and $j$. Let $d(i, j)$ be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points $i$ and $j$ coincide at some moment, the value $d(i, j)$ will be $0$.
Your task is to calculate the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of points.
The second line of the input contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^8$), where $x_i$ is the initial coordinate of the $i$-th point. It is guaranteed that all $x_i$ are distinct.
The third line of the input contains $n$ integers $v_1, v_2, \dots, v_n$ ($-10^8 \le v_i \le 10^8$), where $v_i$ is the speed of the $i$-th point.
-----Output-----
Print one integer — the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Examples-----
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0 | import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
class RangeMinimumQuery:
def __init__(self, n, inf=float("inf")):
self.n0 = 2 ** (n - 1).bit_length()
self.inf = inf
self.data = [self.inf] * (2 * self.n0)
self.cnt = [0] * (2 * self.n0)
def query(self, l, r):
l += self.n0
r += self.n0
res = 0
cnt = 0
while l < r:
if r & 1:
r -= 1
res += self.data[r - 1]
cnt += self.cnt[r - 1]
if l & 1:
res += self.data[l - 1]
cnt += self.cnt[l - 1]
l += 1
l >>= 1
r >>= 1
return res, cnt
def update(self, i, x):
i += self.n0 - 1
self.data[i] += x
self.cnt[i] += 1
while i:
i = ~-i // 2
self.data[i] = self.data[2 * i + 1] + self.data[2 * i + 2]
self.cnt[i] = self.cnt[2 * i + 1] + self.cnt[2 * i + 2]
n = int(input())
xs = list(map(int, input().split()))
vs = list(map(int, input().split()))
xv = [[x, v] for x, v in zip(xs, vs)]
xv.sort()
d = sorted(set(vs))
dd = {j: i for i, j in enumerate(d)}
m = len(dd)
RSQ = RangeMinimumQuery(m, 0)
ans = 0
for x, v in xv:
nv = dd[v]
su, cnt = RSQ.query(0, nv + 1)
ans += cnt * x - su
RSQ.update(nv, x)
print(ans) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
There are $n$ points on a coordinate axis $OX$. The $i$-th point is located at the integer point $x_i$ and has a speed $v_i$. It is guaranteed that no two points occupy the same coordinate. All $n$ points move with the constant speed, the coordinate of the $i$-th point at the moment $t$ ($t$ can be non-integer) is calculated as $x_i + t \cdot v_i$.
Consider two points $i$ and $j$. Let $d(i, j)$ be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points $i$ and $j$ coincide at some moment, the value $d(i, j)$ will be $0$.
Your task is to calculate the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of points.
The second line of the input contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^8$), where $x_i$ is the initial coordinate of the $i$-th point. It is guaranteed that all $x_i$ are distinct.
The third line of the input contains $n$ integers $v_1, v_2, \dots, v_n$ ($-10^8 \le v_i \le 10^8$), where $v_i$ is the speed of the $i$-th point.
-----Output-----
Print one integer — the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Examples-----
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0 | class Points:
def __init__(self, x=0, v=0):
self.x, self.v = x, v
class Fenwick_tree:
def __init__(self, n=0):
self.n = n
self.bit = [0] * (n + 1)
def update(self, x, value):
while x <= self.n:
self.bit[x] += value
x += x & -x
def get(self, x):
sum = 0
while x > 0:
sum += self.bit[x]
x -= x & -x
return sum
n, x, v = int(input()), list(map(int, input().split())), list(map(int, input().split()))
a = [Points(x[i], v[i]) for i in range(n)]
a.sort(key=lambda value: value.x)
v = [a[i].v for i in range(n)]
v.sort()
data = {}
cnt = 1
for i in range(n):
if not data.__contains__(v[i]):
data[v[i]] = cnt
cnt += 1
cnt = Fenwick_tree(n)
sum = Fenwick_tree(n)
res, i = 0, 0
for i in range(n):
a[i].v = data[a[i].v]
res += a[i].x * cnt.get(a[i].v) - sum.get(a[i].v)
cnt.update(a[i].v, 1)
sum.update(a[i].v, a[i].x)
print(res) | CLASS_DEF FUNC_DEF NUMBER NUMBER ASSIGN VAR VAR VAR VAR CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
There are $n$ points on a coordinate axis $OX$. The $i$-th point is located at the integer point $x_i$ and has a speed $v_i$. It is guaranteed that no two points occupy the same coordinate. All $n$ points move with the constant speed, the coordinate of the $i$-th point at the moment $t$ ($t$ can be non-integer) is calculated as $x_i + t \cdot v_i$.
Consider two points $i$ and $j$. Let $d(i, j)$ be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points $i$ and $j$ coincide at some moment, the value $d(i, j)$ will be $0$.
Your task is to calculate the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of points.
The second line of the input contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^8$), where $x_i$ is the initial coordinate of the $i$-th point. It is guaranteed that all $x_i$ are distinct.
The third line of the input contains $n$ integers $v_1, v_2, \dots, v_n$ ($-10^8 \le v_i \le 10^8$), where $v_i$ is the speed of the $i$-th point.
-----Output-----
Print one integer — the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Examples-----
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0 | class Fenwick:
def __init__(self, size):
self.tree = [0] * size
self.size = size
def add(self, idx, delta):
while idx < self.size:
self.tree[idx] += delta
idx = idx + 1 | idx
def getp(self, right):
res = 0
while right != -1:
res += self.tree[right]
right = (right + 1 & right) - 1
return res
def get(self, left, right):
if left > right:
return 0
return self.getp(right) - self.getp(left - 1)
n = int(input())
X = list(map(int, input().split()))
V = list(map(int, input().split()))
XV = list(zip(X, V))
XV.sort()
order = list(range(n))
order.sort(key=lambda i: XV[i][1])
ans = 0
for i in range(n):
ans += XV[n - 1 - i][0] * (n - 1 - 2 * i)
fw = Fenwick(n)
fwcount = Fenwick(n)
for i in order:
ans -= fw.get(i + 1, n - 1) - fwcount.get(i + 1, n - 1) * XV[i][0]
fw.add(i, XV[i][0])
fwcount.add(i, 1)
print(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are $n$ points on a coordinate axis $OX$. The $i$-th point is located at the integer point $x_i$ and has a speed $v_i$. It is guaranteed that no two points occupy the same coordinate. All $n$ points move with the constant speed, the coordinate of the $i$-th point at the moment $t$ ($t$ can be non-integer) is calculated as $x_i + t \cdot v_i$.
Consider two points $i$ and $j$. Let $d(i, j)$ be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points $i$ and $j$ coincide at some moment, the value $d(i, j)$ will be $0$.
Your task is to calculate the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of points.
The second line of the input contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^8$), where $x_i$ is the initial coordinate of the $i$-th point. It is guaranteed that all $x_i$ are distinct.
The third line of the input contains $n$ integers $v_1, v_2, \dots, v_n$ ($-10^8 \le v_i \le 10^8$), where $v_i$ is the speed of the $i$-th point.
-----Output-----
Print one integer — the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Examples-----
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0 | def bitadd(a, w, bit):
x = a
while x <= len(bit) - 1:
bit[x] += w
x += x & -1 * x
def bitsum(a, bit):
ret = 0
x = a
while x > 0:
ret += bit[x]
x -= x & -1 * x
return ret
n = int(input())
x = list(map(int, input().split()))
v = list(map(int, input().split()))
vlis = []
for i in v:
vlis.append(i)
vlis.sort()
vdic = {}
for i in range(n):
vdic[vlis[i]] = i + 1
xv = []
for i in range(n):
xv.append([x[i], v[i]])
xv.sort()
ans = 0
BIT = [0] * (n + 1)
BIT2 = [0] * (n + 1)
for i in range(n):
x, v = xv[i]
ans += x * bitsum(vdic[v], BIT2) - bitsum(vdic[v], BIT)
bitadd(vdic[v], x, BIT)
bitadd(vdic[v], 1, BIT2)
print(ans) | FUNC_DEF ASSIGN VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
There are $n$ points on a coordinate axis $OX$. The $i$-th point is located at the integer point $x_i$ and has a speed $v_i$. It is guaranteed that no two points occupy the same coordinate. All $n$ points move with the constant speed, the coordinate of the $i$-th point at the moment $t$ ($t$ can be non-integer) is calculated as $x_i + t \cdot v_i$.
Consider two points $i$ and $j$. Let $d(i, j)$ be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points $i$ and $j$ coincide at some moment, the value $d(i, j)$ will be $0$.
Your task is to calculate the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of points.
The second line of the input contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^8$), where $x_i$ is the initial coordinate of the $i$-th point. It is guaranteed that all $x_i$ are distinct.
The third line of the input contains $n$ integers $v_1, v_2, \dots, v_n$ ($-10^8 \le v_i \le 10^8$), where $v_i$ is the speed of the $i$-th point.
-----Output-----
Print one integer — the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Examples-----
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0 | def add(sms, cns, n, v, x):
while v < n:
sms[v] += x
cns[v] += 1
v += v & ~(v - 1)
def sumtoo(sms, cns, v):
sm = 0
cn = 0
while v > 0:
sm += sms[v]
cn += cns[v]
v -= v & ~(v - 1)
return sm, cn
def go():
n = int(input())
x = list(map(int, input().split()))
v = list(map(int, input().split()))
sv = sorted(set(v))
sv = {v: k for k, v in enumerate(sv, 1)}
v = [sv[v] for v in v]
a = sorted(((xx, vv) for xx, vv in zip(x, v)), reverse=True)
len_sv = len(sv) + 1
sms = [0] * len_sv
cns = [0] * len_sv
res = 0
sx = 0
for cnt, (xx, vv) in enumerate(a):
sm, cn = sumtoo(sms, cns, vv - 1)
sm, cn = sx - sm, cnt - cn
res += sm - xx * cn
add(sms, cns, len_sv, vv, xx)
sx += xx
return res
print(go()) | FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
There are $n$ points on a coordinate axis $OX$. The $i$-th point is located at the integer point $x_i$ and has a speed $v_i$. It is guaranteed that no two points occupy the same coordinate. All $n$ points move with the constant speed, the coordinate of the $i$-th point at the moment $t$ ($t$ can be non-integer) is calculated as $x_i + t \cdot v_i$.
Consider two points $i$ and $j$. Let $d(i, j)$ be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points $i$ and $j$ coincide at some moment, the value $d(i, j)$ will be $0$.
Your task is to calculate the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of points.
The second line of the input contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^8$), where $x_i$ is the initial coordinate of the $i$-th point. It is guaranteed that all $x_i$ are distinct.
The third line of the input contains $n$ integers $v_1, v_2, \dots, v_n$ ($-10^8 \le v_i \le 10^8$), where $v_i$ is the speed of the $i$-th point.
-----Output-----
Print one integer — the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Examples-----
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0 | import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.readline())
def MI():
return map(int, sys.stdin.readline().split())
def LI():
return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def SI():
return sys.stdin.readline()[:-1]
class BitSum:
def __init__(self, n):
self.n = n + 3
self.table = [0] * (self.n + 1)
def add(self, i, x):
i += 1
while i <= self.n:
self.table[i] += x
i += i & -i
def sum(self, i):
i += 1
res = 0
while i > 0:
res += self.table[i]
i -= i & -i
return res
def main():
n = II()
xx = LI()
vv = LI()
itox = list(sorted(set(xx)))
xtoi = {x: i for i, x in enumerate(itox)}
vx = [(v, x) for x, v in zip(xx, vv)]
cnt = BitSum(len(itox))
val = BitSum(len(itox))
ans = 0
for v, x in sorted(vx):
i = xtoi[x]
if i:
ans += cnt.sum(i - 1) * x - val.sum(i - 1)
cnt.add(i, 1)
val.add(i, x)
print(ans)
main() | IMPORT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
There are $n$ points on a coordinate axis $OX$. The $i$-th point is located at the integer point $x_i$ and has a speed $v_i$. It is guaranteed that no two points occupy the same coordinate. All $n$ points move with the constant speed, the coordinate of the $i$-th point at the moment $t$ ($t$ can be non-integer) is calculated as $x_i + t \cdot v_i$.
Consider two points $i$ and $j$. Let $d(i, j)$ be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points $i$ and $j$ coincide at some moment, the value $d(i, j)$ will be $0$.
Your task is to calculate the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of points.
The second line of the input contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^8$), where $x_i$ is the initial coordinate of the $i$-th point. It is guaranteed that all $x_i$ are distinct.
The third line of the input contains $n$ integers $v_1, v_2, \dots, v_n$ ($-10^8 \le v_i \le 10^8$), where $v_i$ is the speed of the $i$-th point.
-----Output-----
Print one integer — the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Examples-----
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0 | import sys
def main():
import sys
input = sys.stdin.readline
class Bit:
def __init__(self, n):
self.size = n
self.tree = [0] * (n + 1)
def sum(self, i):
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def add(self, i, x):
while i <= self.size:
self.tree[i] += x
i += i & -i
N = int(input())
X = list(map(int, input().split()))
V = list(map(int, input().split()))
info = [(x, v) for x, v in zip(X, V)]
info.sort(key=lambda p: p[0])
info.sort(key=lambda p: p[1])
X.sort()
x2i = {x: (i + 1) for i, x in enumerate(X)}
bit_x = Bit(N + 1)
bit_cnt = Bit(N + 1)
ans = 0
for x, _ in info:
i = x2i[x]
ans += bit_cnt.sum(i) * x - bit_x.sum(i)
bit_x.add(i, x)
bit_cnt.add(i, 1)
print(ans)
main() | IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
There are $n$ points on a coordinate axis $OX$. The $i$-th point is located at the integer point $x_i$ and has a speed $v_i$. It is guaranteed that no two points occupy the same coordinate. All $n$ points move with the constant speed, the coordinate of the $i$-th point at the moment $t$ ($t$ can be non-integer) is calculated as $x_i + t \cdot v_i$.
Consider two points $i$ and $j$. Let $d(i, j)$ be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points $i$ and $j$ coincide at some moment, the value $d(i, j)$ will be $0$.
Your task is to calculate the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of points.
The second line of the input contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^8$), where $x_i$ is the initial coordinate of the $i$-th point. It is guaranteed that all $x_i$ are distinct.
The third line of the input contains $n$ integers $v_1, v_2, \dots, v_n$ ($-10^8 \le v_i \le 10^8$), where $v_i$ is the speed of the $i$-th point.
-----Output-----
Print one integer — the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Examples-----
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0 | import sys
input = sys.stdin.readline
n = int(input())
X = list(map(int, input().split()))
V = list(map(int, input().split()))
XV = [(X[i], V[i]) for i in range(n)]
compression_dict_v = {a: (ind + 2) for ind, a in enumerate(sorted(set(V)))}
XV = [(XV[i][0], compression_dict_v[XV[i][1]]) for i in range(n)]
XV.sort(reverse=True)
LEN = len(compression_dict_v) + 3
BIT1 = [0] * (LEN + 1)
def update1(v, w):
while v <= LEN:
BIT1[v] += w
v += v & -v
def getvalue1(v):
ANS = 0
while v != 0:
ANS += BIT1[v]
v -= v & -v
return ANS
BIT2 = [0] * (LEN + 1)
def update2(v, w):
while v <= LEN:
BIT2[v] += w
v += v & -v
def getvalue2(v):
ANS = 0
while v != 0:
ANS += BIT2[v]
v -= v & -v
return ANS
ANS = 0
for x, v in XV:
ANS += getvalue2(LEN) - getvalue2(v - 1) - (getvalue1(LEN) - getvalue1(v - 1)) * x
update1(v, 1)
update2(v, x)
print(ANS) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
There are $n$ points on a coordinate axis $OX$. The $i$-th point is located at the integer point $x_i$ and has a speed $v_i$. It is guaranteed that no two points occupy the same coordinate. All $n$ points move with the constant speed, the coordinate of the $i$-th point at the moment $t$ ($t$ can be non-integer) is calculated as $x_i + t \cdot v_i$.
Consider two points $i$ and $j$. Let $d(i, j)$ be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points $i$ and $j$ coincide at some moment, the value $d(i, j)$ will be $0$.
Your task is to calculate the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of points.
The second line of the input contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^8$), where $x_i$ is the initial coordinate of the $i$-th point. It is guaranteed that all $x_i$ are distinct.
The third line of the input contains $n$ integers $v_1, v_2, \dots, v_n$ ($-10^8 \le v_i \le 10^8$), where $v_i$ is the speed of the $i$-th point.
-----Output-----
Print one integer — the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Examples-----
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0 | import sys
class SegmentTree:
def __init__(self, N, func, initialRes=0):
self.f = func
self.N = N
self.tree = [(0) for _ in range(4 * self.N)]
self.initialRes = initialRes
def updateTreeNode(self, idx, value):
self.tree[idx + self.N] = value
idx += self.N
i = idx
while i > 1:
self.tree[i >> 1] = self.f(self.tree[i], self.tree[i ^ 1])
i >>= 1
def query(self, l, r):
r += 1
res = self.initialRes
l += self.N
r += self.N
while l < r:
if l & 1:
res = self.f(res, self.tree[l])
l += 1
if r & 1:
r -= 1
res = self.f(res, self.tree[r])
l >>= 1
r >>= 1
return res
def getMaxSegTree(arr):
return SegmentTree(arr, lambda a, b: max(a, b), initialRes=-float("inf"))
def getMinSegTree(arr):
return SegmentTree(arr, lambda a, b: min(a, b), initialRes=float("inf"))
def getSumSegTree(arr):
return SegmentTree(arr, lambda a, b: a + b, initialRes=0)
def main():
n = int(input())
xes = readIntArr()
ves = readIntArr()
xes2 = list(sorted(xes))
xTox2Map = dict()
for i, x in enumerate(xes2):
xTox2Map[x] = i
arr = []
for i in range(n):
arr.append([xes[i], xTox2Map[xes[i]], ves[i]])
arr.sort(key=lambda x: (x[2], x[0]))
st = getSumSegTree(n)
stCnts = getSumSegTree(n)
ans = 0
for originalX, compressedX, v in arr:
smallerSums = st.query(0, compressedX)
smallerCounts = stCnts.query(0, compressedX)
ans += originalX * smallerCounts - smallerSums
st.updateTreeNode(compressedX, originalX)
stCnts.updateTreeNode(compressedX, 1)
print(ans)
return
input = sys.stdin.buffer.readline
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
inf = float("inf")
MOD = 10**9 + 7
main() | IMPORT CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR |
There are $n$ points on a coordinate axis $OX$. The $i$-th point is located at the integer point $x_i$ and has a speed $v_i$. It is guaranteed that no two points occupy the same coordinate. All $n$ points move with the constant speed, the coordinate of the $i$-th point at the moment $t$ ($t$ can be non-integer) is calculated as $x_i + t \cdot v_i$.
Consider two points $i$ and $j$. Let $d(i, j)$ be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points $i$ and $j$ coincide at some moment, the value $d(i, j)$ will be $0$.
Your task is to calculate the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of points.
The second line of the input contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^8$), where $x_i$ is the initial coordinate of the $i$-th point. It is guaranteed that all $x_i$ are distinct.
The third line of the input contains $n$ integers $v_1, v_2, \dots, v_n$ ($-10^8 \le v_i \le 10^8$), where $v_i$ is the speed of the $i$-th point.
-----Output-----
Print one integer — the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Examples-----
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0 | import sys
input = sys.stdin.readline
def getsum(BITTree, i):
s = 0
while i > 0:
s += BITTree[i]
i -= i & -i
return s
def updatebit(BITTree, n, i, v):
while i <= n:
BITTree[i] += v
i += i & -i
n = int(input())
x = [int(i) for i in input().split() if i != "\n"]
v = [int(i) for i in input().split() if i != "\n"]
for i in range(n):
v[i] = [v[i], i]
v.sort()
pos = dict()
store, prev = 1, v[0][0]
for i in range(n):
if i > 0:
if prev == v[i][0]:
pos[x[v[i][1]]] = store
else:
store += 1
pos[x[v[i][1]]] = store
prev = v[i][0]
else:
pos[x[v[i][1]]] = store
sort = sorted(pos.keys())
bit = [0] * (n + 1)
ind = [0] * (n + 1)
ans, count, diff = 0, 0, 0
length = max(sort)
total = 0
for i in range(len(sort) - 1, -1, -1):
count = getsum(bit, pos[sort[i]] - 1)
diff = getsum(ind, pos[sort[i]] - 1)
if i < n - 1:
total += (sort[i + 1] - sort[i]) * (n - 1 - i)
ans += total - (diff - count * sort[i])
updatebit(bit, len(bit) - 1, pos[sort[i]], 1)
updatebit(ind, len(ind) - 1, pos[sort[i]], sort[i])
print(int(ans)) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume Vi and put it in her garden.
Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is Ti, each pile will reduce its volume by Ti. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other.
Note that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.
You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day.
Input
The first line contains a single integer N (1 ≤ N ≤ 105) — the number of days.
The second line contains N integers V1, V2, ..., VN (0 ≤ Vi ≤ 109), where Vi is the initial size of a snow pile made on the day i.
The third line contains N integers T1, T2, ..., TN (0 ≤ Ti ≤ 109), where Ti is the temperature on the day i.
Output
Output a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.
Examples
Input
3
10 10 5
5 7 2
Output
5 12 4
Input
5
30 25 20 15 10
9 10 12 4 13
Output
9 20 35 11 25
Note
In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day. | from sys import stdin
def binary_search(l, r, val):
global prefix
i, j = l, r
while i < j:
mid = (i + j) // 2
ptr = prefix[mid]
if ptr <= val:
i = mid + 1
else:
j = mid
return i
n = int(stdin.buffer.readline())
V = list(map(int, stdin.buffer.readline().split()))
T = list(map(int, stdin.buffer.readline().split()))
cnt = [0] * (n + 1)
res = [0] * (n + 1)
prefix = [0] * (n + 1)
for i in range(n):
if i == 0:
prefix[i] = T[i]
else:
prefix[i] = prefix[i - 1] + T[i]
for i in range(n):
id = None
if i == 0:
id = binary_search(i, n - 1, V[i])
else:
id = binary_search(i, n - 1, V[i] + prefix[i - 1])
if id == n - 1 and V[i] + prefix[i - 1] > prefix[id]:
id += 1
cnt[i] += 1
cnt[id] -= 1
if id == n:
continue
if i == 0:
res[id] += T[id] - prefix[id] + V[i]
else:
res[id] += T[id] - prefix[id] + V[i] + prefix[i - 1]
for i in range(n):
if i > 0:
cnt[i] += cnt[i - 1]
res[i] += cnt[i] * T[i]
for i in range(n):
if i == n - 1:
print(res[i])
else:
print(res[i], end=" ") | FUNC_DEF ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NONE IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING |
Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume Vi and put it in her garden.
Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is Ti, each pile will reduce its volume by Ti. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other.
Note that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.
You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day.
Input
The first line contains a single integer N (1 ≤ N ≤ 105) — the number of days.
The second line contains N integers V1, V2, ..., VN (0 ≤ Vi ≤ 109), where Vi is the initial size of a snow pile made on the day i.
The third line contains N integers T1, T2, ..., TN (0 ≤ Ti ≤ 109), where Ti is the temperature on the day i.
Output
Output a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.
Examples
Input
3
10 10 5
5 7 2
Output
5 12 4
Input
5
30 25 20 15 10
9 10 12 4 13
Output
9 20 35 11 25
Note
In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day. | def bin_search_base(arr, val, base_index):
lo = base_index
hi = len(arr) - 1
while lo < hi:
if hi - lo == 1 and arr[lo] - arr[base_index] < val < arr[hi] - arr[base_index]:
return lo
if val >= arr[hi] - arr[base_index]:
return hi
c = (hi + lo) // 2
if val < arr[c] - arr[base_index]:
hi = c
elif val > arr[c] - arr[base_index]:
lo = c
else:
return c
return lo
n = int(input())
volumes = list(map(int, input().split()))
temps = list(map(int, input().split()))
temp_prefix = [0]
for i in range(n):
temp_prefix.append(temps[i] + temp_prefix[i])
gone_count = [0] * (n + 1)
remaining_count = [0] * n
gone_volume = [0] * (n + 1)
for i in range(n):
j = bin_search_base(temp_prefix, volumes[i], i)
gone_count[j] += 1
gone_volume[j] += volumes[i] - (temp_prefix[j] - temp_prefix[i])
for i in range(n):
remaining_count[i] = 1 + remaining_count[i - 1] - gone_count[i]
for i in range(n):
gone_volume[i] += remaining_count[i] * temps[i]
print(" ".join(map(str, gone_volume[:n]))) | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR IF VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Luba needs your help again! Luba has n TV sets. She knows that i-th TV set will be working from moment of time li till moment ri, inclusive.
Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set.
Help Luba by telling her the index of some redundant TV set. If there is no any, print -1.
Input
The first line contains one integer number n (1 ≤ n ≤ 2·105) — the number of TV sets.
Then n lines follow, each of them containing two integer numbers li, ri (0 ≤ li ≤ ri ≤ 109) denoting the working time of i-th TV set.
Output
If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to n).
If there are multiple answers, print any of them.
Examples
Input
3
1 3
4 6
1 7
Output
1
Input
2
0 10
0 10
Output
1
Input
3
1 2
3 4
6 8
Output
-1
Input
3
1 2
2 3
3 4
Output
2
Note
Consider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one).
Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4]. | n = int(input())
lst = [(-1, -1, 0)]
for i in range(1, n + 1):
li, ri = map(int, input().split())
lst.append((li, ri, i))
lst.append((1000000001, 1000000001, n + 1))
lst.sort()
for i in range(1, n + 1):
tc = lst[i]
tp = lst[i - 1]
tn = lst[i + 1]
if (
tc[1] <= tp[1]
or tc[0] >= tn[0]
and tc[1] <= tn[1]
or tp[1] + 1 >= tn[0]
and tc[1] <= tn[1]
):
print(tc[2])
break
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Luba needs your help again! Luba has n TV sets. She knows that i-th TV set will be working from moment of time li till moment ri, inclusive.
Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set.
Help Luba by telling her the index of some redundant TV set. If there is no any, print -1.
Input
The first line contains one integer number n (1 ≤ n ≤ 2·105) — the number of TV sets.
Then n lines follow, each of them containing two integer numbers li, ri (0 ≤ li ≤ ri ≤ 109) denoting the working time of i-th TV set.
Output
If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to n).
If there are multiple answers, print any of them.
Examples
Input
3
1 3
4 6
1 7
Output
1
Input
2
0 10
0 10
Output
1
Input
3
1 2
3 4
6 8
Output
-1
Input
3
1 2
2 3
3 4
Output
2
Note
Consider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one).
Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4]. | n = int(input())
L = []
for i in range(n):
a, b = map(int, input().split())
L.append((a, b, i))
L.sort()
if n == 1:
print(-1)
else:
temp = -2
for i in range(0, len(L)):
if i == 0:
if L[i][0] == L[i + 1][0]:
if L[i][1] <= L[i + 1][1]:
temp = L[i][2]
else:
temp = L[i + 1][2]
break
elif i > 0 and i < len(L) - 1:
if L[i][1] <= L[i - 1][1]:
temp = L[i][2]
break
elif L[i][0] == L[i + 1][0] and L[i][1] <= L[i + 1][1]:
temp = L[i][2]
break
elif (
L[i][0] <= L[i - 1][1]
and L[i + 1][0] <= L[i - 1][1] + 1
and L[i + 1][1] >= L[i][1]
):
temp = L[i][2]
break
elif L[i][0] > L[i - 1][1] and L[i + 1][0] <= L[i][0]:
temp = L[i][2]
break
elif L[i][1] <= L[i - 1][1]:
temp = L[i][2]
break
print(temp + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Luba needs your help again! Luba has n TV sets. She knows that i-th TV set will be working from moment of time li till moment ri, inclusive.
Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set.
Help Luba by telling her the index of some redundant TV set. If there is no any, print -1.
Input
The first line contains one integer number n (1 ≤ n ≤ 2·105) — the number of TV sets.
Then n lines follow, each of them containing two integer numbers li, ri (0 ≤ li ≤ ri ≤ 109) denoting the working time of i-th TV set.
Output
If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to n).
If there are multiple answers, print any of them.
Examples
Input
3
1 3
4 6
1 7
Output
1
Input
2
0 10
0 10
Output
1
Input
3
1 2
3 4
6 8
Output
-1
Input
3
1 2
2 3
3 4
Output
2
Note
Consider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one).
Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4]. | import sys
input = sys.stdin.readline
tv = []
n = int(input())
for i in range(n):
l, r = map(int, input().split())
tv.append([l, r, i + 1])
tv.sort()
ans = -1
r = tv[0][1]
l = tv[0][0]
tmp = tv[0][2]
for i in range(1, n):
if tv[i][0] <= l and tv[i][1] >= r:
ans = tmp
break
if tv[i][1] <= r:
ans = tv[i][2]
break
else:
l = max(r + 1, tv[i][0])
r = tv[i][1]
tmp = tv[i][2]
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Luba needs your help again! Luba has n TV sets. She knows that i-th TV set will be working from moment of time li till moment ri, inclusive.
Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set.
Help Luba by telling her the index of some redundant TV set. If there is no any, print -1.
Input
The first line contains one integer number n (1 ≤ n ≤ 2·105) — the number of TV sets.
Then n lines follow, each of them containing two integer numbers li, ri (0 ≤ li ≤ ri ≤ 109) denoting the working time of i-th TV set.
Output
If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to n).
If there are multiple answers, print any of them.
Examples
Input
3
1 3
4 6
1 7
Output
1
Input
2
0 10
0 10
Output
1
Input
3
1 2
3 4
6 8
Output
-1
Input
3
1 2
2 3
3 4
Output
2
Note
Consider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one).
Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4]. | n = int(input())
lis = []
for i in range(n):
a, b = map(int, input().split())
lis.append([a, b, i + 1])
lis.sort()
i = 1
ans = -1
while i < n:
if lis[i][1] <= lis[i - 1][1]:
ans = lis[i][2]
print(ans)
exit(0)
elif lis[i][0] <= lis[i - 1][0] and lis[i][1] >= lis[i - 1][1]:
ans = lis[i - 1][2]
print(ans)
exit(0)
lis[i][0] = max(lis[i][0], lis[i - 1][1] + 1)
i += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
Example:
matrix = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
],
k = 8,
return 13.
Note:
You may assume k is always valid, 1 ≤ k ≤ n2. | class Solution:
def kthSmallest(self, matrix, k):
list = matrix[0]
for i in range(1, len(matrix)):
list.extend(matrix[i])
list.sort()
return list[k - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR BIN_OP VAR NUMBER |
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
Example:
matrix = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
],
k = 8,
return 13.
Note:
You may assume k is always valid, 1 ≤ k ≤ n2. | class Solution:
def kthSmallest(self, matrix, k):
arr = []
for i in matrix:
for j in i:
arr.append(j)
arr.sort()
print(arr)
return arr[k - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR BIN_OP VAR NUMBER |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sweetness, n, k):
l = 0
h = sum(sweetness)
while l <= h:
mid = (l + h) // 2
piece, sweet = 0, 0
for x in sweetness:
sweet += x
if sweet >= mid:
piece += 1
sweet = 0
if piece < k + 1:
h = mid - 1
else:
l = mid + 1
return h | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sweetness, n, k):
start = min(sweetness)
end = sum(sweetness)
while start < end - 1:
mid = (start + end) // 2
r = self.get(sweetness, mid, k)
if r:
end = mid - 1
else:
start = mid
if not self.get(sweetness, end, k):
return end
return start
def get(self, arr, mid, k):
count = 0
s = 0
for item in arr:
if s + item >= mid:
s = 0
count += 1
else:
s += item
if count >= k + 1:
return False
return True | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sweetness, n, k):
def find(sweet, key, k):
count = 0
summ = 0
for i in sweet:
summ += i
if summ >= key:
summ = 0
count += 1
return count
l = 0
r = sum(sweetness)
while l <= r:
mid = (l + r) // 2
check = find(sweetness, mid, k)
if check < k + 1:
r = mid - 1
else:
l = mid + 1
return r | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def check(self, mid, arr, N, K):
sum = 0
count = 0
for i in range(N):
sum += arr[i]
if sum >= mid:
count += 1
sum = 0
return count >= K + 1
def maxSweetness(self, sweetness, N, K):
start = 1
end = 100000000000000.0
ans = -1
while start <= end:
mid = (start + end) // 2
if self.check(mid, sweetness, N, K):
ans = mid
start = mid + 1
else:
end = mid - 1
return int(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN 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 VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sweetness, n, k):
lb = min(sweetness)
ub = sum(sweetness)
def slices(candidate):
current, number_of_slices = 0, 0
for i in range(len(sweetness)):
current += sweetness[i]
if current >= candidate:
number_of_slices += 1
current = 0
return number_of_slices
while lb + 1 < ub:
mid = (lb + ub) // 2
n_slices = slices(mid)
if n_slices >= k + 1:
lb = mid
else:
ub = mid
if slices(ub) == k + 1:
return ub
return lb | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN VAR |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sweetness, n, k):
low, ans = 0, 0
high = 1000000000000000.0
while low <= high:
mid = (low + high) // 2
curSum, pieces = 0, 0
for i in sweetness:
curSum = curSum + i
if curSum >= mid:
curSum = 0
pieces = pieces + 1
if pieces >= k + 1:
ans = mid
low = mid + 1
else:
high = mid - 1
return int(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sweetness, n, k):
def count_pieces(sweetness, target):
count = pieces = 0
for s in sweetness:
count += s
if count >= target:
pieces += 1
count = 0
return pieces
left, right = 1, sum(sweetness) // (k + 1)
result = 0
while left <= right:
mid = left + (right - left) // 2
if count_pieces(sweetness, mid) >= k + 1:
result = mid
left = mid + 1
else:
right = mid - 1
return result | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP 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 VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sweetness, n, k):
def tolerable(val):
sweet = 0
count = 0
for i in sweetness:
sweet += i
if sweet >= val:
count += 1
sweet = 0
if count >= k + 1:
return True
else:
return False
lo = min(sweetness)
hi = sum(sweetness) + 1
while lo < hi:
mid = (lo + hi) // 2
if tolerable(mid) == False:
hi = mid
else:
lo = mid + 1
return lo - 1 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sweetness, n, k):
left = min(sweetness)
right = sum(sweetness)
while left < right:
mid = (left + right + 1) // 2
curr_sum = 0
cuts = 0
for i in range(n):
curr_sum += sweetness[i]
if curr_sum >= mid:
cuts += 1
curr_sum = 0
if cuts >= k + 1:
left = mid
else:
right = mid - 1
return left | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sweetness, n, k):
def validSweetness(mid):
total_pieces = 0
curr_sweetness = 0
for s in sweetness:
curr_sweetness += s
if curr_sweetness >= mid:
total_pieces += 1
curr_sweetness = 0
return total_pieces >= k + 1
left = min(sweetness)
right = sum(sweetness)
while left < right:
mid = (left + right + 1) // 2
if validSweetness(mid):
left = mid
else:
right = mid - 1
return left | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL 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 |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sweetness, n, k):
def good(x, sweetness, k):
n = len(sweetness)
s, cnt = 0, 0
for i in range(0, n):
s += sweetness[i]
if s >= x:
s = 0
cnt += 1
return cnt >= k + 1
low, high = 0, 1000000000.0
ans = -1
while low <= high:
mid = (low + high) // 2
if good(mid, sweetness, k):
low = mid + 1
ans = mid
else:
high = mid - 1
return int(ans) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER 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 BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def solve(self, v, target, k):
fq = 0
sm = 0
n = len(v)
for i in range(n):
if sm >= target:
fq += 1
sm = 0
sm += v[i]
if sm >= target:
fq += 1
return fq >= k
def maxSweetness(self, sweetness, N, K):
left = 1
right = int(1000000000.0)
ans = 0
while right >= left:
mid = (left + right) // 2
if self.solve(sweetness, mid, K + 1):
ans = mid
left = mid + 1
else:
right = mid - 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def can_provide(self, this_much, arr, k):
person = total = 0
for i in arr:
total += i
if total >= this_much:
person += 1
total = 0
return person >= k + 1
def maxSweetness(self, sweetness, n, k):
low = min(sweetness)
high = sum(sweetness)
ans = 0
while low <= high:
mid = (low + high) // 2
if self.can_provide(mid, sweetness, k):
ans = mid
low = mid + 1
else:
high = mid - 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR 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 |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def solve(self, val, arr, k):
n = len(arr)
sumo = 0
for i in range(n):
if sumo >= val:
sumo = 0
k -= 1
sumo += arr[i]
if sumo >= val:
k -= 1
if k > -1:
return False
return True
def maxSweetness(self, sweetness, n, k):
l = 0
r = sum(sweetness)
while l < r - 1:
mid = (l + r) // 2
if self.solve(mid, sweetness, k):
l = mid
else:
r = mid - 1
while r:
if self.solve(r, sweetness, k):
return r
r -= 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF 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 BIN_OP VAR NUMBER WHILE VAR IF FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR NUMBER |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sweetness, n, k):
minimumSweetness = min(sweetness)
maximumSweetness = sum(sweetness)
low = minimumSweetness
high = maximumSweetness
result = -1
while low <= high:
mid = low + (high - low >> 1)
if self.isChunksPossible(sweetness, mid, k):
result = mid
low = mid + 1
else:
high = mid - 1
return result
def isChunksPossible(self, sweetness, geeksBite, k):
chunkSweetness = 0
count = 0
for i in range(0, len(sweetness)):
chunkSweetness += sweetness[i]
if chunkSweetness >= geeksBite:
count += 1
chunkSweetness = 0
return count >= k + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN 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 VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR BIN_OP VAR NUMBER |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sweetness, n, k):
def giveans(val):
s = 0
count = 0
ans = float("inf")
for i in sweetness:
s += i
if s > val:
s = 0
count += 1
if count < k + 1:
return 1
return 0
l = 1
h = sum(sweetness)
res = 0
while l <= h:
m = (l + h) // 2
if giveans(m):
res = m
h = m - 1
else:
l = m + 1
return res | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING 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 NUMBER ASSIGN VAR FUNC_CALL VAR VAR 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 |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def canProvide(self, mid, sweetness, k):
curr_total = 0
person = 0
for i in range(0, len(sweetness)):
curr_total += sweetness[i]
if curr_total >= mid:
person += 1
curr_total = 0
return person >= k + 1
def maxSweetness(self, sweetness, n, k):
low = float("inf")
high = 0
for i in range(0, n):
low = min(low, sweetness[i])
high += sweetness[i]
ans = 0
while low <= high:
mid = low + (high - low) // 2
if self.canProvide(mid, sweetness, k):
low = mid + 1
ans = mid
else:
high = mid - 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN 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 VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sweetness, n, k):
left, right = min(sweetness), sum(sweetness) // (k + 1)
while left < right:
mid = (left + right + 1) // 2
count, curr_sum = 0, 0
for s in sweetness:
curr_sum += s
if curr_sum >= mid:
count += 1
curr_sum = 0
if count >= k + 1:
left = mid
else:
right = mid - 1
return left | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sw, n, k):
def check(mid):
peices = cur = 0
for i in sw:
cur += i
if cur >= mid:
cur = 0
peices += 1
return peices > k
l = 0
h = sum(sw)
res = -1
while l < h:
mid = (l + h) // 2
if check(mid):
l = mid + 1
res = mid
else:
h = mid - 1
return res + 1 if check(res + 1) else res | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR 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 VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | def isPossible(arr, mid, n, k):
cnt = 1
current_Sum = 0
for i in range(n):
if current_Sum + arr[i] <= mid:
current_Sum += arr[i]
else:
cnt += 1
current_Sum = 0
if cnt > k + 1:
return False
return True
class Solution:
def maxSweetness(self, sweetness, n, k):
low, high = 0, sum(sweetness)
while low <= high:
mid = (low + high) // 2
if isPossible(sweetness, mid, n, k):
high = mid - 1
else:
low = mid + 1
return low | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR 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 BIN_OP VAR NUMBER RETURN VAR |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sweetness, n, k):
l = 0
a = sweetness
if k == 0:
return sum(a)
r = sum(a)
res = -1
while r >= l:
m = (l + r) // 2
ct = 1
ans = 0
ans1 = 10**18
for i in range(n):
if ct > k:
ans += a[i]
continue
if ans >= m:
ans1 = min(ans1, ans)
ans = a[i]
ct += 1
else:
ans += a[i]
if ct > k + 1 or ct > k and ans >= m:
ans1 = min(ans1, ans)
res = ans1
l = m + 1
else:
r = m - 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Geek is organizing a birthday party, so his friends brought a cake for him. The cake consists of N chunks, whose individual sweetness is represented by the sweetness array. Now at the time of distribution, Geek cuts the cake into K + 1 pieces to distribute among his K friends. One piece he took for himself. Each piece consists of some consecutive chunks. He is very kind, so he left the piece with minimum sweetness for him.
You need to find the maximum sweetness that the Geek can get if he distributes the cake optimally.
Example 1:
Input:
N = 6, K = 2
sweetness[] = {6, 3, 2, 8, 7, 5}
Output:
9
Explanation:
Geek can divide the cake to [6, 3], [2, 8], [7, 5]
with sweetness level 9, 10 and 12 respectively.
Example 2:
Input:
N = 7, K = 3
sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output:
7
Explanation:
Geek can divide the cake to [1, 2, 4], [7], [3, 6], [9]
with sweetness level 7, 7, 9 and 9 respectively.
Your Task:
You need to complete the maxSweetness() function which takes an integer array of sweetness, an integer N and an integer K as the input parameters and returns an integer denoting the maximum sweetness that the Geek can get.
Expected Time Complexity: O(NlogM), where M is the sum of elements in the array.
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= K < N
1 <= sweetness[i] <= 10^{9} | class Solution:
def maxSweetness(self, sw, n, k):
l = 1
r = 10**9
ans = 0
while l <= r:
m = (l + r) // 2
p = 0
cur = 0
for i in range(0, n):
cur += sw[i]
if cur >= m:
p += 1
cur = 0
if p >= k + 1:
ans = m
l = m + 1
else:
r = m - 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given an array of size N, find the smallest positive integer value that is either not presented in the array or cannot be represented as a sum(coz sum means you are adding two or more elements) of some elements from the array.
Example 1:
Input:
N = 6
array[] = {1, 10, 3, 11, 6, 15}
Output:
2
Explanation:
2 is the smallest integer value that cannot
be represented as sum of elements from the array.
Example 2:
Input:
N = 3
array[] = {1, 1, 1}
Output:
4
Explanation:
1 is present in the array.
2 can be created by combining two 1s.
3 can be created by combining three 1s.
4 is the smallest integer value that cannot be
represented as sum of elements from the array.
Your Task:
You don't need to read input or print anything. Complete the function smallestpositive() which takes the array and N as input parameters and returns the smallest positive integer value that cannot be represented as the sum of some elements from the array.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
1 ≤ array[i] ≤ 10^{9}
The array may contain duplicates. | class Solution:
def smallestpositive(self, array, n):
array.sort()
smallest_int = 1
for i in range(n):
if array[i] <= smallest_int:
smallest_int += array[i]
else:
return smallest_int
return smallest_int | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN VAR RETURN VAR |
Given an array of size N, find the smallest positive integer value that is either not presented in the array or cannot be represented as a sum(coz sum means you are adding two or more elements) of some elements from the array.
Example 1:
Input:
N = 6
array[] = {1, 10, 3, 11, 6, 15}
Output:
2
Explanation:
2 is the smallest integer value that cannot
be represented as sum of elements from the array.
Example 2:
Input:
N = 3
array[] = {1, 1, 1}
Output:
4
Explanation:
1 is present in the array.
2 can be created by combining two 1s.
3 can be created by combining three 1s.
4 is the smallest integer value that cannot be
represented as sum of elements from the array.
Your Task:
You don't need to read input or print anything. Complete the function smallestpositive() which takes the array and N as input parameters and returns the smallest positive integer value that cannot be represented as the sum of some elements from the array.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
1 ≤ array[i] ≤ 10^{9}
The array may contain duplicates. | class Solution:
def smallestpositive(self, a, n):
ans = 1
a.sort()
for i in range(n):
if ans < a[i]:
return ans
ans += a[i]
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR VAR RETURN VAR |
Given an array of size N, find the smallest positive integer value that is either not presented in the array or cannot be represented as a sum(coz sum means you are adding two or more elements) of some elements from the array.
Example 1:
Input:
N = 6
array[] = {1, 10, 3, 11, 6, 15}
Output:
2
Explanation:
2 is the smallest integer value that cannot
be represented as sum of elements from the array.
Example 2:
Input:
N = 3
array[] = {1, 1, 1}
Output:
4
Explanation:
1 is present in the array.
2 can be created by combining two 1s.
3 can be created by combining three 1s.
4 is the smallest integer value that cannot be
represented as sum of elements from the array.
Your Task:
You don't need to read input or print anything. Complete the function smallestpositive() which takes the array and N as input parameters and returns the smallest positive integer value that cannot be represented as the sum of some elements from the array.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
1 ≤ array[i] ≤ 10^{9}
The array may contain duplicates. | class Solution:
def smallestpositive(self, a, n):
a.sort()
res = 1
for i in range(n):
if res < a[i]:
return res
else:
res = res + a[i]
return res | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR |
Given an array of size N, find the smallest positive integer value that is either not presented in the array or cannot be represented as a sum(coz sum means you are adding two or more elements) of some elements from the array.
Example 1:
Input:
N = 6
array[] = {1, 10, 3, 11, 6, 15}
Output:
2
Explanation:
2 is the smallest integer value that cannot
be represented as sum of elements from the array.
Example 2:
Input:
N = 3
array[] = {1, 1, 1}
Output:
4
Explanation:
1 is present in the array.
2 can be created by combining two 1s.
3 can be created by combining three 1s.
4 is the smallest integer value that cannot be
represented as sum of elements from the array.
Your Task:
You don't need to read input or print anything. Complete the function smallestpositive() which takes the array and N as input parameters and returns the smallest positive integer value that cannot be represented as the sum of some elements from the array.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
1 ≤ array[i] ≤ 10^{9}
The array may contain duplicates. | class Solution:
def smallestpositive(self, array, n):
maxi = 1
array.sort()
if array[0] != 1 or n == 0:
return 1
for i in range(1, n):
if array[i] > maxi + 1:
return maxi + 1
maxi += array[i]
return maxi + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR NUMBER |
Given an array of size N, find the smallest positive integer value that is either not presented in the array or cannot be represented as a sum(coz sum means you are adding two or more elements) of some elements from the array.
Example 1:
Input:
N = 6
array[] = {1, 10, 3, 11, 6, 15}
Output:
2
Explanation:
2 is the smallest integer value that cannot
be represented as sum of elements from the array.
Example 2:
Input:
N = 3
array[] = {1, 1, 1}
Output:
4
Explanation:
1 is present in the array.
2 can be created by combining two 1s.
3 can be created by combining three 1s.
4 is the smallest integer value that cannot be
represented as sum of elements from the array.
Your Task:
You don't need to read input or print anything. Complete the function smallestpositive() which takes the array and N as input parameters and returns the smallest positive integer value that cannot be represented as the sum of some elements from the array.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
1 ≤ array[i] ≤ 10^{9}
The array may contain duplicates. | class Solution:
def smallestpositive(self, array, n):
array.sort()
max_val = 0
sum_val = [0] * (n + 1)
for i in range(1, n + 1):
sum_val[i] = sum_val[i - 1] + array[i - 1]
for i in range(n):
if array[i] > max_val + 1:
return max_val + 1
max_val = max(max_val, sum_val[i + 1])
return max_val + 1 | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER |
Given an array of size N, find the smallest positive integer value that is either not presented in the array or cannot be represented as a sum(coz sum means you are adding two or more elements) of some elements from the array.
Example 1:
Input:
N = 6
array[] = {1, 10, 3, 11, 6, 15}
Output:
2
Explanation:
2 is the smallest integer value that cannot
be represented as sum of elements from the array.
Example 2:
Input:
N = 3
array[] = {1, 1, 1}
Output:
4
Explanation:
1 is present in the array.
2 can be created by combining two 1s.
3 can be created by combining three 1s.
4 is the smallest integer value that cannot be
represented as sum of elements from the array.
Your Task:
You don't need to read input or print anything. Complete the function smallestpositive() which takes the array and N as input parameters and returns the smallest positive integer value that cannot be represented as the sum of some elements from the array.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
1 ≤ array[i] ≤ 10^{9}
The array may contain duplicates. | from itertools import combinations
class Solution:
def smallestpositive(self, array, n):
array.sort()
mis = 1
for i in range(n):
if mis < array[i]:
return mis
else:
mis = mis + array[i]
return mis
def smallestpositive1(self, array, n):
result = 0
possibles = set()
for i in range(1, n + 1):
possible_numbers = combinations(array, i)
for item in possible_numbers:
possibles.add(sum(item))
x = min(min(array), 1)
while x in possibles:
x = x + 1
return x | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given an array of size N, find the smallest positive integer value that is either not presented in the array or cannot be represented as a sum(coz sum means you are adding two or more elements) of some elements from the array.
Example 1:
Input:
N = 6
array[] = {1, 10, 3, 11, 6, 15}
Output:
2
Explanation:
2 is the smallest integer value that cannot
be represented as sum of elements from the array.
Example 2:
Input:
N = 3
array[] = {1, 1, 1}
Output:
4
Explanation:
1 is present in the array.
2 can be created by combining two 1s.
3 can be created by combining three 1s.
4 is the smallest integer value that cannot be
represented as sum of elements from the array.
Your Task:
You don't need to read input or print anything. Complete the function smallestpositive() which takes the array and N as input parameters and returns the smallest positive integer value that cannot be represented as the sum of some elements from the array.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
1 ≤ array[i] ≤ 10^{9}
The array may contain duplicates. | class Solution:
def smallestpositive(self, a, n):
a.sort()
s = 0
for i in range(n):
if a[i] > s + 1:
return s + 1
s += a[i]
return s + 1 | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR NUMBER |
Given an array of size N, find the smallest positive integer value that is either not presented in the array or cannot be represented as a sum(coz sum means you are adding two or more elements) of some elements from the array.
Example 1:
Input:
N = 6
array[] = {1, 10, 3, 11, 6, 15}
Output:
2
Explanation:
2 is the smallest integer value that cannot
be represented as sum of elements from the array.
Example 2:
Input:
N = 3
array[] = {1, 1, 1}
Output:
4
Explanation:
1 is present in the array.
2 can be created by combining two 1s.
3 can be created by combining three 1s.
4 is the smallest integer value that cannot be
represented as sum of elements from the array.
Your Task:
You don't need to read input or print anything. Complete the function smallestpositive() which takes the array and N as input parameters and returns the smallest positive integer value that cannot be represented as the sum of some elements from the array.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
1 ≤ array[i] ≤ 10^{9}
The array may contain duplicates. | class Solution:
def smallestpositive(self, array, n):
arr = sorted(array)
max_sum = 0
for i in arr:
if i <= max_sum + 1:
max_sum += i
else:
return max_sum + 1
return max_sum + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER |
Given an array of size N, find the smallest positive integer value that is either not presented in the array or cannot be represented as a sum(coz sum means you are adding two or more elements) of some elements from the array.
Example 1:
Input:
N = 6
array[] = {1, 10, 3, 11, 6, 15}
Output:
2
Explanation:
2 is the smallest integer value that cannot
be represented as sum of elements from the array.
Example 2:
Input:
N = 3
array[] = {1, 1, 1}
Output:
4
Explanation:
1 is present in the array.
2 can be created by combining two 1s.
3 can be created by combining three 1s.
4 is the smallest integer value that cannot be
represented as sum of elements from the array.
Your Task:
You don't need to read input or print anything. Complete the function smallestpositive() which takes the array and N as input parameters and returns the smallest positive integer value that cannot be represented as the sum of some elements from the array.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
1 ≤ array[i] ≤ 10^{9}
The array may contain duplicates. | class Solution:
def smallestpositive(self, array, n):
array.sort()
j = 1
sum = 1
for i in range(n):
if sum < array[i]:
return sum
else:
sum += array[i]
return sum | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR VAR RETURN VAR |
Given an array of size N, find the smallest positive integer value that is either not presented in the array or cannot be represented as a sum(coz sum means you are adding two or more elements) of some elements from the array.
Example 1:
Input:
N = 6
array[] = {1, 10, 3, 11, 6, 15}
Output:
2
Explanation:
2 is the smallest integer value that cannot
be represented as sum of elements from the array.
Example 2:
Input:
N = 3
array[] = {1, 1, 1}
Output:
4
Explanation:
1 is present in the array.
2 can be created by combining two 1s.
3 can be created by combining three 1s.
4 is the smallest integer value that cannot be
represented as sum of elements from the array.
Your Task:
You don't need to read input or print anything. Complete the function smallestpositive() which takes the array and N as input parameters and returns the smallest positive integer value that cannot be represented as the sum of some elements from the array.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
1 ≤ array[i] ≤ 10^{9}
The array may contain duplicates. | class Solution:
def smallestpositive(self, arr, n):
arr.sort()
if arr[0] != 1:
return 1
cur = 1
for i in range(n):
if arr[i] <= cur:
cur += arr[i]
else:
break
return cur | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN VAR |
Given an array of size N, find the smallest positive integer value that is either not presented in the array or cannot be represented as a sum(coz sum means you are adding two or more elements) of some elements from the array.
Example 1:
Input:
N = 6
array[] = {1, 10, 3, 11, 6, 15}
Output:
2
Explanation:
2 is the smallest integer value that cannot
be represented as sum of elements from the array.
Example 2:
Input:
N = 3
array[] = {1, 1, 1}
Output:
4
Explanation:
1 is present in the array.
2 can be created by combining two 1s.
3 can be created by combining three 1s.
4 is the smallest integer value that cannot be
represented as sum of elements from the array.
Your Task:
You don't need to read input or print anything. Complete the function smallestpositive() which takes the array and N as input parameters and returns the smallest positive integer value that cannot be represented as the sum of some elements from the array.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
1 ≤ array[i] ≤ 10^{9}
The array may contain duplicates. | class Solution:
def smallestpositive(self, array, n):
res_arr = [0] * n
array.sort()
sum = array[0]
if array[0] != 1:
return 1
for idx in range(1, n):
if array[idx] - sum > 1:
return sum + 1
sum += array[idx]
return sum + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR NUMBER |
Given an array of size N, find the smallest positive integer value that is either not presented in the array or cannot be represented as a sum(coz sum means you are adding two or more elements) of some elements from the array.
Example 1:
Input:
N = 6
array[] = {1, 10, 3, 11, 6, 15}
Output:
2
Explanation:
2 is the smallest integer value that cannot
be represented as sum of elements from the array.
Example 2:
Input:
N = 3
array[] = {1, 1, 1}
Output:
4
Explanation:
1 is present in the array.
2 can be created by combining two 1s.
3 can be created by combining three 1s.
4 is the smallest integer value that cannot be
represented as sum of elements from the array.
Your Task:
You don't need to read input or print anything. Complete the function smallestpositive() which takes the array and N as input parameters and returns the smallest positive integer value that cannot be represented as the sum of some elements from the array.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
1 ≤ array[i] ≤ 10^{9}
The array may contain duplicates. | class Solution:
def smallestpositive(self, array, n):
array.sort()
res = 1
i = 0
while i < n and array[i] <= res:
res = res + array[i]
i += 1
return res | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR |
Given an array of size N, find the smallest positive integer value that is either not presented in the array or cannot be represented as a sum(coz sum means you are adding two or more elements) of some elements from the array.
Example 1:
Input:
N = 6
array[] = {1, 10, 3, 11, 6, 15}
Output:
2
Explanation:
2 is the smallest integer value that cannot
be represented as sum of elements from the array.
Example 2:
Input:
N = 3
array[] = {1, 1, 1}
Output:
4
Explanation:
1 is present in the array.
2 can be created by combining two 1s.
3 can be created by combining three 1s.
4 is the smallest integer value that cannot be
represented as sum of elements from the array.
Your Task:
You don't need to read input or print anything. Complete the function smallestpositive() which takes the array and N as input parameters and returns the smallest positive integer value that cannot be represented as the sum of some elements from the array.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
1 ≤ array[i] ≤ 10^{9}
The array may contain duplicates. | class Solution:
def smallestpositive(self, arr, n):
smallest = 1
arr.sort()
for i in range(len(arr)):
if arr[i] <= smallest:
smallest += arr[i]
else:
return smallest
return smallest | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN VAR RETURN VAR |
Given an array of size N, find the smallest positive integer value that is either not presented in the array or cannot be represented as a sum(coz sum means you are adding two or more elements) of some elements from the array.
Example 1:
Input:
N = 6
array[] = {1, 10, 3, 11, 6, 15}
Output:
2
Explanation:
2 is the smallest integer value that cannot
be represented as sum of elements from the array.
Example 2:
Input:
N = 3
array[] = {1, 1, 1}
Output:
4
Explanation:
1 is present in the array.
2 can be created by combining two 1s.
3 can be created by combining three 1s.
4 is the smallest integer value that cannot be
represented as sum of elements from the array.
Your Task:
You don't need to read input or print anything. Complete the function smallestpositive() which takes the array and N as input parameters and returns the smallest positive integer value that cannot be represented as the sum of some elements from the array.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
1 ≤ array[i] ≤ 10^{9}
The array may contain duplicates. | class Solution:
def smallestpositive(self, array, n):
array.sort()
s = 0
sa = 1
for i in range(n):
s = s + array[i]
if sa >= array[i] or sa >= s:
sa += array[i]
else:
return sa
return s + 1 | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR 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:
position = sorted(position)
lo = 1
hi = position[-1]
while lo < hi:
mid = lo + (hi - lo) // 2 + 1
if self.can_be_put(position, m, mid):
lo = mid
else:
hi = mid - 1
return lo
def can_be_put(self, position, m, force):
next_pos = position[0]
for i in range(len(position)):
if next_pos <= position[i]:
m -= 1
next_pos = position[i] + force
return m <= 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR 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 FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN 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 is_force_valid(self, position: List[int], m: int, force: int) -> bool:
last_pos = position[0]
m -= 1
for i in range(1, len(position)):
if position[i] - last_pos >= force:
m -= 1
if m == 0:
return True
last_pos = position[i]
return False
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
left = 1
right = position[-1] - position[0]
while right > left:
mid = (right + left + 1) // 2
if self.is_force_valid(position, m, mid):
left = mid
else:
right = mid - 1
return left | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER 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 VAR VAR RETURN NUMBER VAR FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR 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 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, A: List[int], m: int) -> int:
A.sort()
n = len(A)
l, r = 1, A[-1] - A[0]
def get_m(d):
res = 1
i = 1
prev = A[0]
while i < n:
if A[i] - prev >= d:
res += 1
prev = A[i]
i += 1
return res
while l < r:
mid = r - (r - l) // 2
if get_m(mid) < m:
r = mid - 1
else:
l = mid
return l | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR 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 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 good(d):
c, prev = 1, 0
for i in range(1, len(position)):
if position[i] - position[prev] >= d:
c += 1
prev = i
if c == 0:
break
return c
position.sort()
l, r = 0, position[-1] - position[0]
while l <= r:
mid = (r + l) // 2
if good(mid) >= m:
l = mid + 1
else:
r = mid - 1
return r | 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 IF VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER 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 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 isvalid(gap, m):
j = 0
for i in range(len(position)):
if position[i] - position[j] >= gap:
m -= 1
j = i
if m == 0:
return True
return False
low, high = 1, position[-1]
sem = 0
while low <= high:
mid = (high + low) // 2
if isvalid(mid, m - 1):
sem = mid
low = mid + 1
else:
high = mid - 1
return sem | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER 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 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)
high = position[-1] - position[0]
if m == 2:
return high
low = high
for i in range(1, len(position)):
low = min(position[i] - position[i - 1], low)
if m == len(position):
return low
def count(step):
c = 1
pivot = 0
for i in range(1, len(position)):
if step <= position[i] - position[pivot]:
c += 1
pivot = i
return c
res = None
while low <= high:
mid = low + (high - low) // 2
num = count(mid)
if num >= m:
res = mid
low = mid + 1
if num < m:
high = mid - 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR NONE WHILE VAR VAR ASSIGN VAR BIN_OP 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 IF 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()
left, right = 0, position[-1] - position[0]
while left <= right:
mid = (left + right) // 2
count = 1
curr = position[0]
for x in position[1:]:
if x - curr >= mid:
count += 1
curr = x
if count >= m:
left = mid + 1
else:
right = mid - 1
return right | 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 BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF 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 fit_balls(self, positions, n, min_dist):
last_position = positions[0]
placement_count = 1
for p in positions:
if p - last_position >= min_dist:
last_position = p
placement_count += 1
if placement_count >= n:
return True
return False
def maxDistance(self, position: List[int], m: int) -> int:
pos = sorted(position)
lo = 0
hi = pos[-1] - pos[0]
while lo < hi:
mid = hi - (hi - lo) // 2
can_fit = self.fit_balls(pos, m, mid)
if not can_fit:
hi = mid - 1
else:
lo = mid
return lo | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR 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 VAR VAR VAR IF 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 = 0, position[-1] - position[0]
def possible(target, m):
idx = prev = 0
while m:
if idx >= len(position):
return False
if not prev or position[idx] >= prev + target:
m -= 1
prev = position[idx]
else:
idx += 1
return True
while lo < hi:
mid = (lo + hi + 1) // 2
if possible(mid, m):
lo = mid
else:
hi = mid - 1
return lo | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER WHILE VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN 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, pos: List[int], m: int) -> int:
pos.sort()
n = len(pos)
def check(gap):
i = 0
ii = 0
nn = 1
while i < n:
if pos[i] - pos[ii] >= gap:
nn += 1
ii = i
i += 1
return nn < m
l = float("inf")
for i in range(n - 1):
l = min(l, pos[i + 1] - pos[i])
r = pos[-1] - pos[0] + 1
while l < r:
mid = (l + r) // 2
if check(mid):
r = mid
else:
l = mid + 1
return l - 1 | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR 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:
sorted_position = sorted(position)
N = len(position)
lo = 1
hi = sorted_position[N - 1] - sorted_position[0]
last_achieved = -1
while lo <= hi:
mid = (lo + hi) // 2
if self._try_target(mid, sorted_position, m):
lo = mid + 1
last_achieved = mid
else:
hi = mid - 1
return last_achieved
def _try_target(self, target: int, positions: List[int], m: int) -> bool:
slotted = 1
last_slotted = positions[0]
for i in range(1, len(positions)):
if positions[i] - last_slotted >= target:
slotted += 1
last_slotted = positions[i]
return slotted >= m | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP 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 BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR 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 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 posb(t):
k = 0
s = 0
for i in range(n - 1):
k += position[i + 1] - position[i]
if k >= t:
k = 0
s += 1
return s >= m - 1
hgh = position[-1] - position[0]
low = 0
while low < hgh:
mid = hgh - (hgh - low) // 2
if posb(mid):
low = mid
else:
hgh = mid - 1
return low | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP 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 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 helper(d):
cnt = 1
cur = position[0]
for i in range(1, len(position)):
if cur + d <= position[i]:
cur = position[i]
cnt += 1
return cnt
l, r = 0, position[-1] - position[0]
res = -float("inf")
while l <= r:
mid = l + r >> 1
if helper(mid) == m:
res = max(mid, res)
l = mid + 1
elif helper(mid) > m:
l = mid + 1
else:
r = mid - 1
return max(res, l - 1) | CLASS_DEF FUNC_DEF VAR 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 BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING 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 IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL 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()
def check(min_force):
count = 1
k = position[0]
for n in position:
if n - k >= min_force:
count += 1
k = n
if count >= m:
return True
return False
i, j = 1, position[-1] - position[0]
while i <= j:
mid = (i + j) // 2
c1 = check(mid)
c2 = check(mid + 1)
if c1 and not c2:
return mid
if not c1:
j = mid - 1
else:
i = mid + 1 | 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 BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER 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:
def isValid(f, k):
pos = position[0]
count = 1
for i in range(1, n):
if position[i] - pos >= f:
pos = position[i]
count += 1
if count == k:
return True
return False
n = len(position)
position.sort()
res = 0
l, r = 0, position[n - 1] - position[0] + 1
while l < r:
mid = l + r >> 1
if isValid(mid, m):
res = max(res, mid)
l = mid + 1
else:
r = mid
return res | 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 EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR 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 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(dist):
prev = position[0]
cnt = 1
idx = 0
while idx < len(position):
curr = position[idx]
if curr - prev >= dist:
cnt += 1
prev = curr
idx += 1
if cnt > m:
break
return cnt
position.sort()
L = 1
R = position[-1] - position[0]
while L < R:
if L == R - 1:
if check(R) >= m:
return R
else:
return L
mid = L + (R - L) // 2
if check(mid) < m:
R = mid - 1
else:
L = mid
return L | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN 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 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 = 1
hi = position[-1]
while lo < hi:
dist = (hi + lo) // 2 + 1
if self.maxBalls(position, dist) >= m:
lo = dist
else:
hi = dist - 1
return lo
def maxBalls(self, sorted_position: List[int], distance: int) -> int:
l_ind = 0
r_ind = 1
count = 1
while r_ind < len(sorted_position):
if sorted_position[r_ind] - sorted_position[l_ind] >= distance:
count += 1
l_ind = r_ind
r_ind += 1
return count | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL 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 VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR 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 working(mid: int):
prev_stall = 0
stall = 1
for i in range(1, m):
while position[stall] - position[prev_stall] < mid:
stall += 1
if stall == len(position):
return False
prev_stall = stall
return True
L = max(position)
low = 1
high = int(L / (m - 1))
largest = 0
position = sorted(position)
while low <= high:
mid = int((low + high) / 2)
if working(mid):
if mid < int(L / (m - 1)):
low = mid + 1
else:
largest = max(largest, mid)
break
largest = max(largest, mid)
elif mid > 0:
high = mid - 1
return largest | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF 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 | def solve(positions, m, target):
p = positions[0]
m -= 1
for p2 in positions[1:]:
if p2 - p >= target:
m -= 1
p = p2
if m == 0:
return True
return False
class Solution:
def maxDistance(self, positions: List[int], m: int) -> int:
positions.sort()
l, r = 0, positions[-1] - positions[0]
best = 0
while l <= r:
target = (l + r) // 2
if solve(positions, m, target):
l = target + 1
best = max(best, target)
else:
r = target - 1
return best | FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER CLASS_DEF 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 VAR VAR NUMBER IF FUNC_CALL 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()
res = 0
l, r = 1, (position[-1] - position[0]) // (m - 1)
while l <= r:
mid = (l + r) // 2
prev = position[0]
ball = 1
for i in position[1:]:
if i - prev >= mid:
ball += 1
prev = i
if ball == m:
break
if ball == m:
l = mid + 1
res = mid
else:
r = mid - 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR 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 VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR IF VAR VAR 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 maxDistance(self, position: List[int], m: int) -> int:
position = sorted(position)
if m == 2:
return position[-1] - position[0]
def solve(threshold, m):
last_ball_pos = position[0]
for pos in position[1:]:
if pos - last_ball_pos >= threshold:
m -= 1
last_ball_pos = pos
if m == 0:
return True
return False
start = 0
end = position[-1] - position[0]
res = 0
while start <= end:
middle = (start + end) // 2
if solve(middle, m - 1):
start = middle + 1
res = max(res, middle)
else:
end = middle - 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF 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 VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER 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()
def possible(d):
cnt = 1
cur = position[0]
for i in range(1, len(position)):
if position[i] - cur >= d:
cnt += 1
cur = position[i]
if cnt >= m:
return True
return False
lo = 0
hi = position[-1] - position[0]
while lo < hi:
mid = hi - (hi - lo) // 2
if possible(mid):
lo = mid
else:
hi = mid - 1
return lo | CLASS_DEF FUNC_DEF VAR 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 BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER 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 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()
l, r, sol = 0, position[-1] - position[0], -1
def helper(target):
pre, cnt = position[0], 1
for n in position:
temp = n - pre
if temp >= target:
cnt += 1
pre = n
if position[-1] - pre >= target:
cnt += 1
if cnt >= m:
return True
return False
while l <= r:
mid = (l + r) // 2
if helper(mid):
l = mid + 1
else:
r = mid - 1
return l - 1 | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER 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 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()
return self.binaryS(0, position[-1], position, m)
def binaryS(self, left, right, position, m):
while left < right:
mid = (left + right) // 2
val = self.check(position, mid + 1, m)
if val:
left = mid + 1
else:
right = mid
return right
def check(self, position, interval, m) -> bool:
total = 1
start = position[0]
for i in range(1, len(position)):
if position[i] - start >= interval:
total += 1
start = position[i]
return total >= m | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN 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 ASSIGN VAR VAR VAR RETURN VAR VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.