Your browser is not supported. Please, update your browser or switch to a different one. Learn more about which browsers are supported.
Task description
You are given integers K, M and a non-empty array A consisting of N integers. Every element of the array is not greater than M.
You should divide this array into K blocks of consecutive elements. The size of the block is any integer between 0 and N. Every element of the array should belong to some block.
The sum of the block from X to Y equals A[X] + A[X + 1] + ... + A[Y]. The sum of empty block equals 0.
The large sum is the maximal sum of any block.
For example, you are given integers K = 3, M = 5 and array A such that:
A[0] = 2 A[1] = 1 A[2] = 5 A[3] = 1 A[4] = 2 A[5] = 2 A[6] = 2The array can be divided, for example, into the following blocks:
- [2, 1, 5, 1, 2, 2, 2], [], [] with a large sum of 15;
- [2], [1, 5, 1, 2], [2, 2] with a large sum of 9;
- [2, 1, 5], [], [1, 2, 2, 2] with a large sum of 8;
- [2, 1], [5, 1], [2, 2, 2] with a large sum of 6.
The goal is to minimize the large sum. In the above example, 6 is the minimal large sum.
Write a function:
def solution(K, M, A)
that, given integers K, M and a non-empty array A consisting of N integers, returns the minimal large sum.
For example, given K = 3, M = 5 and array A such that:
A[0] = 2 A[1] = 1 A[2] = 5 A[3] = 1 A[4] = 2 A[5] = 2 A[6] = 2the function should return 6, as explained above.
Write an efficient algorithm for the following assumptions:
- N and K are integers within the range [1..100,000];
- M is an integer within the range [0..10,000];
- each element of array A is an integer within the range [0..M].
Task timeline
def blocksNo(A, maxBlock):
# Initially set the A[0] being an individual block
blocksNumber = 1 # The number of blocks, that A could
# be divided to with the restriction
# that, the sum of each block is less
# than or equal to maxBlock
preBlockSum = A[0]
for element in A[1:]:
# Try to extend the previous block
if preBlockSum + element > maxBlock:
# Fail to extend the previous block, because
# of the sum limitation maxBlock
preBlockSum = element
blocksNumber += 1
else:
preBlockSum += element
return blocksNumber
def solution(K, A):
blocksNeeded = 0 # Given the restriction on the sum of
# each block, how many blocks could
# the original A be divided to?
resultLowerBound = max(A)
resultUpperBound = sum(A)
result = 0 # Minimal large sum
# Handle two special cases
if K == 1: return resultUpperBound
if K >= len(A): return resultLowerBound
# Binary search the result
while resultLowerBound <= resultUpperBound:
resultMaxMid = (resultLowerBound + resultUpperBound) / 2
blocksNeeded = blocksNo(A, resultMaxMid)
if blocksNeeded <= K:
# With large sum being resultMaxMid or resultMaxMid-,
# we need blocksNeeded/blocksNeeded- blocks. While we
# have some unused blocks (K - blocksNeeded), We could
# try to use them to decrease the large sum.
resultUpperBound = resultMaxMid - 1
result = resultMaxMid
else:
# With large sum being resultMaxMid or resultMaxMid-,
# we need to use more than K blocks. So resultMaxMid
# is impossible to be our answer.
resultLowerBound = resultMaxMid + 1
return result
Traceback (most recent call last): File "user.py", line 133, in <module> result = solution ( K, M, A ) TypeError: solution() takes exactly 2 arguments (3 given)
def blocksNo(A, maxBlock):
# Initially set the A[0] being an individual block
blocksNumber = 1 # The number of blocks, that A could
# be divided to with the restriction
# that, the sum of each block is less
# than or equal to maxBlock
preBlockSum = A[0]
for element in A[1:]:
# Try to extend the previous block
if preBlockSum + element > maxBlock:
# Fail to extend the previous block, because
# of the sum limitation maxBlock
preBlockSum = element
blocksNumber += 1
else:
preBlockSum += element
return blocksNumber
def solution(K, M, A):
blocksNeeded = 0 # Given the restriction on the sum of
# each block, how many blocks could
# the original A be divided to?
resultLowerBound = M
resultUpperBound = sum(A)
result = 0 # Minimal large sum
# Handle two special cases
if K == 1: return resultUpperBound
if K >= len(A): return resultLowerBound
# Binary search the result
while resultLowerBound <= resultUpperBound:
resultMaxMid = (resultLowerBound + resultUpperBound) / 2
blocksNeeded = blocksNo(A, resultMaxMid)
if blocksNeeded <= K:
# With large sum being resultMaxMid or resultMaxMid-,
# we need blocksNeeded/blocksNeeded- blocks. While we
# have some unused blocks (K - blocksNeeded), We could
# try to use them to decrease the large sum.
resultUpperBound = resultMaxMid - 1
result = resultMaxMid
else:
# With large sum being resultMaxMid or resultMaxMid-,
# we need to use more than K blocks. So resultMaxMid
# is impossible to be our answer.
resultLowerBound = resultMaxMid + 1
return result
def blocksNo(A, maxBlock):
# Initially set the A[0] being an individual block
blocksNumber = 1 # The number of blocks, that A could
# be divided to with the restriction
# that, the sum of each block is less
# than or equal to maxBlock
preBlockSum = A[0]
for element in A[1:]:
# Try to extend the previous block
if preBlockSum + element > maxBlock:
# Fail to extend the previous block, because
# of the sum limitation maxBlock
preBlockSum = element
blocksNumber += 1
else:
preBlockSum += element
return blocksNumber
def solution(K, M, A):
blocksNeeded = 0 # Given the restriction on the sum of
# each block, how many blocks could
# the original A be divided to?
resultLowerBound = M
resultUpperBound = sum(A)
result = 0 # Minimal large sum
# Handle two special cases
if K == 1: return resultUpperBound
if K >= len(A): return resultLowerBound
# Binary search the result
while resultLowerBound <= resultUpperBound:
resultMaxMid = (resultLowerBound + resultUpperBound) / 2
blocksNeeded = blocksNo(A, resultMaxMid)
if blocksNeeded <= K:
# With large sum being resultMaxMid or resultMaxMid-,
# we need blocksNeeded/blocksNeeded- blocks. While we
# have some unused blocks (K - blocksNeeded), We could
# try to use them to decrease the large sum.
resultUpperBound = resultMaxMid - 1
result = resultMaxMid
else:
# With large sum being resultMaxMid or resultMaxMid-,
# we need to use more than K blocks. So resultMaxMid
# is impossible to be our answer.
resultLowerBound = resultMaxMid + 1
return result
def blocksNo(A, maxBlock):
# Initially set the A[0] being an individual block
blocksNumber = 1 # The number of blocks, that A could
# be divided to with the restriction
# that, the sum of each block is less
# than or equal to maxBlock
preBlockSum = A[0]
for element in A[1:]:
# Try to extend the previous block
if preBlockSum + element > maxBlock:
# Fail to extend the previous block, because
# of the sum limitation maxBlock
preBlockSum = element
blocksNumber += 1
else:
preBlockSum += element
return blocksNumber
def solution(K, M, A):
blocksNeeded = 0 # Given the restriction on the sum of
# each block, how many blocks could
# the original A be divided to?
resultLowerBound = M
resultUpperBound = sum(A)
result = 0 # Minimal large sum
# Handle two special cases
if K == 1: return resultUpperBound
if K >= len(A): return resultLowerBound
# Binary search the result
while resultLowerBound <= resultUpperBound:
resultMaxMid = (resultLowerBound + resultUpperBound) / 2
blocksNeeded = blocksNo(A, resultMaxMid)
if blocksNeeded <= K:
# With large sum being resultMaxMid or resultMaxMid-,
# we need blocksNeeded/blocksNeeded- blocks. While we
# have some unused blocks (K - blocksNeeded), We could
# try to use them to decrease the large sum.
resultUpperBound = resultMaxMid - 1
result = resultMaxMid
else:
# With large sum being resultMaxMid or resultMaxMid-,
# we need to use more than K blocks. So resultMaxMid
# is impossible to be our answer.
resultLowerBound = resultMaxMid + 1
return result
The following issues have been detected: wrong answers.