def solve_time_out(self, cipher):
        A = cipher
        L = [-1 for _ in A]
        S = [-1 for _ in A]
        for i in xrange(len(A) - 2, -1, -1):
            idx = i + 1
            while idx != -1:
                if A[idx] < A[i]:
                    idx = L[idx]
                else:
                    break
            L[i] = idx

            idx = i + 1
            while idx != -1:
                if A[idx] > A[i]:
                    idx = S[idx]
                else:
                    break
            S[i] = idx

        cnt = 0
        for i in xrange(len(A)):
            cnt += 1
            l = L[i]
            s = S[i]
            while l != -1 and (s == -1 or s > l):
                cnt += 1
                l = L[l]

        return cnt
chaining >, dp
dataset/ETHPy150Open algorhythms/HackerRankAlgorithms/Almost sorted interval.py/Solution.solve_time_out