Algorithms and Data Structures/Coding Practices

AlgoExpert Monotonic Array

brightlightkim 2022. 7. 21. 09:35

def isMonotonic(array):
    firstChangeIndex = 1
    isIncreasing = True
    for i in range(1, len(array)):
        if (i == firstChangeIndex):
            if array[i] < array[i-1]:
                isIncreasing = False
                firstChangeIndex = -1
            elif array[i] > array[i-1]:
                isIncreasing = True
                firstChangeIndex = -1
        if firstChangeIndex != -1:
            firstChangeIndex += 1
        
        else:
            if (array[i] > array[i-1] and not isIncreasing) or (array[i] < array[i-1] and isIncreasing):
                return False
    return True

'Algorithms and Data Structures > Coding Practices' 카테고리의 다른 글

AlgoExpert First Duplicate Value  (0) 2022.07.22
AlgoExpert Array of Products  (0) 2022.07.22
AlgoExpert Move Element to End  (0) 2022.07.20
Kattis Above Average  (0) 2022.07.20
Kattis a different problem  (0) 2022.07.20