Introfor

[Intro] almostIncreasingSequence 본문

Programming_prob/Codesignal

[Intro] almostIncreasingSequence

YongArtist 2020. 7. 18. 21:15

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.

Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence containing only one element is also considered to be strictly increasing.

일련의 증가하는 시퀀스를 가지는가를 판별하는 문제다.

도저히 알고리즘이 생각나지 않아 타인의 코드를 가지고 왔다.

def almostIncreasingSequence(sequence):
    droppped = False
    last = prev = min(sequence)-1
    for elm in sequence:
        if elm <= last:
            if droppped:
                return False
            else:
                droppped = True
            if elm <= prev:
                prev = last
            elif elm >= prev:
                prev = last = elm
        else:
            prev, last = last, elm
    return True

 

'Programming_prob > Codesignal' 카테고리의 다른 글

[Intro] All Longest Strings  (0) 2020.07.18
[Intro] matrixElementsSum  (0) 2020.07.18
[Intro] Make Array Consecutive 2  (0) 2020.07.18
[Intro] shapeArea  (0) 2020.07.18
[Intro] adjacentElementsProduct  (0) 2020.07.18
Comments