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.
- Prepare
- Algorithms
- Search
- Minimum Loss
- Discussions
Minimum Loss
Minimum Loss
Sort by
recency
|
393 Discussions
|
Please Login in order to post a comment
When you need to sort the data but keep track of the original index positions, use a pair-index structure. In the code below, the 'quickSort()` routine sorts by price in non-increasing order and for elements that have have the same price, it sorts by index in increasing order.
Java solution with
TreeSet
that provides ahigher
function to query the closest higher element.def minimumLoss(price): valores = []
for pv in range(len(price)): for pi in range(pv + 1, len(price)): rest = price[pv] - price[pi] if rest > 0: valores.append(rest)
Java:
Python Solution: