Solve almostIncreasingSequence (Codefights)

后端 未结 15 1197
谎友^
谎友^ 2020-12-08 07:35

Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.

15条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 08:20

    This works on most cases except has problems with performance.

    def almostIncreasingSequence(sequence):
        if len(sequence)==2:
            return sequence==sorted(list(sequence))
        else:
            for i in range(0,len(sequence)):
                newsequence=sequence[:i]+sequence[i+1:]
                if (newsequence==sorted(list(newsequence))) and len(newsequence)==len(set(newsequence)):
                    return True
                    break
                else:
                    result=False
        return result
    

提交回复
热议问题