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.
Since similar solutions rely on either a sorted array or using binary search n times where n is the length of the array, the time complexity comes out to O(nlogn) either way, I figured this is more intuitive:
defminimumLoss(price):# Write your code herep_to_i={}fori,pinenumerate(price):p_to_i[p]=iprice.sort()min_value=math.infforiinrange(1,len(price)):ifp_to_i[price[i-1]]>p_to_i[price[i]]:min_value=min(min_value,price[i]-price[i-1])returnmin_value
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Loss 1
You are viewing a single comment's thread. Return to all comments →
Since similar solutions rely on either a sorted array or using binary search
n
times wheren
is the length of the array, the time complexity comes out toO(nlogn)
either way, I figured this is more intuitive: