简体   繁体   English

如何减少列表遍历器的执行时间限制

[英]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.我正在处理关于代码信号的 Python 问题,我试图查看给定列表是否是一个严格递增的序列,当只删除一个元素时。 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.所以我构建了代码,在 for 循环中,从列表中删除元素i并检查它是否是一个递增序列,然后在该确切索引处替换该元素并重新开始。 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])不起作用(例如 [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]).相反,如果遇到不增加,我们需要涵盖两种可能性:删除当前数字(例如 [1,2,1,3])或删除前一个(例如 [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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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