简体   繁体   中英

How to decrease execution time limit for a list-traverser

I'm working on a Python problem on Code Signal and I'm trying to see if a given list is a strictly increasing sequence when only one element is removed from it. So I've built code that, in a for-loop, removes element i from the list and checks if it is an increasing sequence and then replaces that element at that exact index and starts over. This is the code:

def almostIncreasingSequence(sequence):

    for i in range(len(sequence)):
        element = sequence[i]
        del sequence[i]

        if all(i < j for i, j in zip(sequence, sequence[1:])):
            return True
        sequence.insert(i, element)

    return False

The code works well but it causes an error with the execution time. Is there any way I can improve this existing code so it runs faster?

It would be quicker to run through the list, comparing each value to the previous to ensure it's strictly increasing. Allow this to not be true for one number in the list and skip this number. Unfortunately it's not quite that simple, as we can see below:

Won't work (eg. [1,4,2,3])

def almostIncreasingSequence(sequence):
    lastValue = sequence[0]
    removed_value = False
    for i in range(1,len(sequence)):
        if sequence[i] <= lastValue:
            if removed_value:
                return False
            else:
                removed_value = True
        else:
            lastValue = sequence[i]
    return True

Instead, we need to cover the two possibilities if we encounter a non-increase: remove the current number (eg. [1,2,1,3]) or remove the previous (eg. [1,2,8,4]). We also have some edge cases for removing the first or last number in the list.

Final (not so pretty) solution

def almostIncreasingSequence(sequence):
    lastValue = sequence[0]
    skipped_value = False
    for i in range(1,len(sequence)):
        if sequence[i] <= lastValue:
            if i+1 == len(sequence):
                return not skipped_value # last number is not decreasing, skip if we can
            if skipped_value: 
                # if we've already skipped a number - won't work
                return False
            elif sequence[i+1] > sequence[i-1]:
                # skipping the current number will fix it
                skipped_value = True
                lastValue = sequence[i-1]
            else:
                # try and skip the previous number
                skipped_value = True
                if i == 1 or sequence[i] > sequence[i-2]:
                    # can skip the previous number and it'll work
                    lastValue = sequence[i]
                else:
                    # we have no chance
                    return False  
        else:
            lastValue = sequence[i]
    return True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM