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.
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
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 →
!/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)}