• + 0 comments

    My Solution in Python using Dynamic Programing

    def poisonousPlants(p):
        vet = [float('inf')]*len(p)
        ind = [-1]*len(p)
        aux = 0
        dias = 0
        for i in range(1, len(p)):
            j = i - 1
            Achou = False
            aux = 0
            while not Achou:
                if p[j] >= p[i]:
                    if vet[j] == float('inf'):
                        Achou = True
                    else:
                        aux = vet[j]
                        j = ind[j]
                else:
                    if vet[j] > aux:
                        vet[i] = aux + 1
                        dias = max(dias, vet[i])
                        ind[i] = j
                        Achou = True
                    else:
                        j = ind[j]
        return dias