Solve almostIncreasingSequence (Codefights)

后端 未结 15 1256
谎友^
谎友^ 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:05

    Below is the Python3 code that I used and it worked fine:

    def almostIncreasingSequence(sequence):
    flag = False
    
    if(len(sequence) < 3):
        return True
    
    if(sequence == sorted(sequence)):
        if(len(sequence)==len(set(sequence))):
            return True
    
    bigFlag = True
    for i in range(len(sequence)):
        if(bigFlag and i < len(sequence)-1 and sequence[i] < sequence[i+1]):
            bigFlag = True
            continue
        tempSeq = sequence[:i] + sequence[i+1:]
        if(tempSeq == sorted(tempSeq)):
            if(len(tempSeq)==len(set(tempSeq))):
                flag = True
                break
        bigFlag = False
    return flag
    

提交回复
热议问题