• + 0 comments

    !/bin/python3

    import math import os import random import re import sys

    def minimumLoss(price): n = len(price) # Create a dictionary of price to year index price_index = {p: i for i, p in enumerate(price)}

    # Sort prices ascendingly
    sorted_prices = sorted(price)
    
    min_loss = float('inf')
    
    # Check adjacent prices in sorted list
    for i in range(1, n):
        high = sorted_prices[i]
        low = sorted_prices[i - 1]
    
        # Buy (high price) must be before sell (low price)
        if price_index[high] < price_index[low]:
            min_loss = min(min_loss, high - low)
    
    return min_loss