We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defminimumLoss(price):n=len(price)# Create a list of (price, original index) tuplesprice_with_indices=[(price[i],i)foriinrange(n)]# Sort the list by priceprice_with_indices.sort()min_loss=float('inf')# Iterate through the sorted prices and calculate the minimum lossforiinrange(1,n):current_price,current_index=price_with_indices[i]previous_price,previous_index=price_with_indices[i-1]# Ensure that we buy before we sellifcurrent_index<previous_index:min_loss=min(min_loss,current_price-previous_price)returnmin_loss
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Loss
You are viewing a single comment's thread. Return to all comments →