You are viewing a single comment's thread. Return to all comments →
Java solution with TreeSet that provides a higher function to query the closest higher element.
TreeSet
higher
public static int minimumLoss(List<Long> prices) { var sortedPrices = new TreeSet<Long>(); var minimumLoss = (long) Integer.MAX_VALUE; for (var price : prices) { var higher = sortedPrices.higher(price); if (higher != null) { minimumLoss = Math.min(minimumLoss, higher - price); } sortedPrices.add(price); } return (int) minimumLoss; }
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 →
Java solution with
TreeSet
that provides ahigher
function to query the closest higher element.