[codility]MinMaxDivision

Python代码如下(最近在熟悉Python所以接下来的解题报告会尽量使用Python):

def solution(K, A):
    # write your code in Python 2.6
    # each time given an expected minimum sum 'ems',
    # then get the minimum group number 'mgn' we should divide into to meet the 'ems'
    # if 'mgn' <= 'K', then try to decrease 'ems'; else versa;
    left = max(A)
    right = sum(A)
    while left <= right:
        ems = (right-left)/2+left
        rmgn = getMinimumGroupNumber(A, ems)
        if rmgn <= K:# we can decrease 'ems'
            right = ems-1
        else:# we should increase 'ems' to decrease 'rmgn' 
            left = ems+1
    return left

def getMinimumGroupNumber(A, ems):
    rmgn = 0
    curSum = 0
    for x in A:
        curSum += x
        if curSum > ems:
            curSum = x
            rmgn += 1
    if curSum > 0:
        rmgn += 1
    return rmgn


你可能感兴趣的:([codility]MinMaxDivision)