Minimum Loss 1

  • + 0 comments
    def minimumLoss(prices):
        from bisect import bisect_left
        
        min_loss = None
        visited = [prices[0]]
        
        for i in range(1, len(prices)):
            price = prices[i]
            j = bisect_left(visited, price)
            visited.insert(j, price)
            if j + 1 == len(visited):
                continue
            loss = visited[j + 1] - visited[j]
            min_loss = min(min_loss, loss) if min_loss else loss
        return min_loss