Sort by

recency

|

393 Discussions

|

  • + 0 comments

    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.

    typedef struct {
        long long price;
        int index;
    } Pair;
    
    void quickSort(void **a, int lo, int hi);
    
    int minimumLoss(long long* price, int n) {
        long long minLoss = 1e16;
        Pair *pair[n], p[n];
        for (int i = 0; i < n; ++i) {
            pair[i] = &p[i];
            p[i].price = price[i];
            p[i].index = i;
        }
        quickSort((void**) pair, 0, n-1);
        for (int i = 0; i < n-1; ++i) {
            if (pair[i]->index > pair[i+1]->index) continue;
            long long loss = pair[i]->price - pair[i+1]->price;
            if (loss < minLoss) minLoss = loss;
        }
        return minLoss;
    }
    
  • + 0 comments

    Java solution with TreeSet that provides a higher function to query the closest higher element.

        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;
        }
    
  • + 0 comments

    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)

    return min(valores)
    
  • + 0 comments

    Java:

    public static int minimumLoss(List<Long> price) {
        // Create a sorted copy of the prices list
        List<Long> sorted = new ArrayList<>(price);
        Collections.sort(sorted);
    
        // Map to store the original index of each price
        HashMap<Long, Integer> IndexMap = new HashMap<>();
        for (int i = 0; i < price.size(); i++) {
          IndexMap.put(price.get(i), i);
        }
    
        long minLoss = Long.MAX_VALUE;
    
        // Iterate through the sorted prices to calculate the minimum loss
        for (int i = 0; i < sorted.size() - 1; i++) {
          Long next = sorted.get(i + 1);
          Long current = sorted.get(i);
          // Check if the "next" price comes after the "current" price in the
          // original list
          if (next - current < minLoss
              && IndexMap.get(next) < IndexMap.get(current)) {
            minLoss = next - current;
          }
        }
    
        return (int) minLoss;
      }
    }
    
      }
    }
    
  • + 0 comments

    Python Solution:

    def minimumLoss(price):
        arr = sorted(enumerate(price), key=lambda x: x[1], reverse=True)
        diff = [arr[i][1] - arr[i + 1][1] for i in range(len(arr) - 1) if arr[i][0] < arr[i + 1][0]]
        return max(min(diff), 0)