Python-如何找到最长的单调递增数字的长度?

时间:2015-10-25 22:34:53

标签: python

如何查找给定列表中持续时间最长的单调增长的长度?

例如,如果L = [10, 4, 6, 8, 3, 4, 5, 7, 7, 2]那么函数实现应返回值5,因为L中单调增加的整数运行时间最长为[3, 4, 5, 7, 7]

请建议我解决方案。

5 个答案:

答案 0 :(得分:2)

保持跟踪单调递增数字的count和跟踪最高count的“maxCont”

def mCount(L):
    count=1

    maxCount=0
    for i in range(len(L)-1):
        if L[i+1] >= L[i]:
            count +=1
        else:

            count =1
        if maxCount<count:
            maxCount=count
    return maxCount

演示:

print mCount([10, 4, 6, 8, 3, 4, 5, 7, 7, 2])

输出:

5

答案 1 :(得分:1)

def longest_run(L):
    """
    Assumes L is a list of integers containing at least 2 elements.
    Finds the longest run of numbers in L, where the longest run can
    either be monotonically increasing or monotonically decreasing. 
    In case of a tie for the longest run, choose the longest run 
    that occurs first.
    Does not modify the list.
    Returns the sum of the longest run. 
    """
    firstone = mIncreasing(L)
    secondone = mDecreasing(L)
    result = 0
    if len(firstone) > len(secondone) :
        for i in firstone:
            result += i
    elif len(firstone) == len(secondone) :
        for i in L:
            if sum(firstone) == sum(secondone):
                for r in firstone:
                    result += r 
                break
            if i in firstone and not i in secondone:
                for j in firstone:
                    result += j
                break
            elif i in secondone and not i in firstone:
                for k in secondone:
                    result += k
                break

    elif len(firstone) < len(secondone):
        for i in secondone:
            result += i
    return result

#finde the longest run of monotonically increasing numbers in L

def mIncreasing(L):
    current_set = L[:]
    temp_set = [current_set[0]]
    m_increasing = []

    for i in range(len(current_set)-1):
        if current_set[i] <= current_set[i+1]:
            temp_set.append(current_set[i+1])
            if len(temp_set) > len(m_increasing):
                m_increasing = temp_set[:]
        elif current_set[i] > current_set[i+1]:
            temp_set = [current_set[i+1]]
    return m_increasing


#finde the longest run of monotonically decreasing numbers in L
def mDecreasing(L):
    current_set = L[:]
    temp_set = [current_set[0]]
    m_decreasing = []
    for i in range(len(current_set)-1):
        if current_set[i] >= current_set[i+1]:
            temp_set.append(current_set[i+1])
            if len(temp_set) > len(m_decreasing):
                m_decreasing = temp_set[:]
        elif current_set[i] < current_set[i+1]:
            temp_set = [current_set[i+1]]
    return m_decreasing

答案 2 :(得分:0)

def longestRun(L):
    answer = 0
    start = 0
    end = 0
    while start<len(L) and end<len(L):
        if L[end] >= L[start]:
            end += 1
        else:
            if end - start + 1 > answer:
                answer = end - start
                start = end
    return answer

答案 3 :(得分:0)

def longestRun(L):

def longestRun(L):

count=1

maxCount=1
for i in range(len(L)-1):
    if L[i+1] >= L[i]:
        count +=1
    else:

        count =1
    if maxCount<count:
        maxCount=count
return maxCount

答案 4 :(得分:0)

def newIndexIsLongerLength(oldStartIndex, oldEndIndex, newStartIndex, newEndIndex):
    return (newEndIndex-newStartIndex) > (oldEndIndex-oldStartIndex)

def getMonotonicallyI(L):
    startIndex = 0
    endIndex = 0

    for tmpStartIndex in range(len(L)-1):
        tmpEndIndex = tmpStartIndex

        while tmpEndIndex < len(L)-1:
            if L[tmpEndIndex] > L[tmpEndIndex+1]:
                break
            tmpEndIndex += 1

        if newIndexIsLongerLength(startIndex, endIndex, tmpStartIndex, tmpEndIndex):
            startIndex, endIndex = tmpStartIndex, tmpEndIndex

    return startIndex, endIndex


def getMonotonicallyD(L):
    startIndex = len(L)-1
    endIndex = len(L)-1

    for tmpStartIndex in range(len(L)-1, 0, -1):
        tmpEndIndex = tmpStartIndex

        while tmpEndIndex > 0:
            if L[tmpEndIndex] > L[tmpEndIndex-1]:
                break
            tmpEndIndex -= 1

        if newIndexIsLongerLength(endIndex, startIndex, tmpEndIndex, tmpStartIndex):
            startIndex, endIndex = tmpStartIndex, tmpEndIndex

    #print(endIndex, startIndex)
    return endIndex, startIndex


def longest_run(L):
    """
    Assumes L is a list of integers containing at least 2 elements.
    Finds the longest run of numbers in L, where the longest run can
    either be monotonically increasing or monotonically decreasing. 
    In case of a tie for the longest run, choose the longest run 
    that occurs first.
    Does not modify the list.
    Returns the sum of the longest run. 
    """

    # Starting Values Of Index


    startIndexI, endIndexI = getMonotonicallyI(L)
    startIndexD, endIndexD = getMonotonicallyD(L)

    if newIndexIsLongerLength(startIndexI, endIndexI, startIndexD, endIndexD):
        return sum(L[startIndexD:endIndexD+1])
    return sum(L[startIndexI:endIndexI+1])