bestpair

Benchmark created by Carroll Yu on


Test runner

Ready to run.

Testing in
TestOps/sec
a
    function stock_picker(stockPrices) {
      var profitDays = [];
      var bestProfit = 0;

      for (x = 0; x < stockPrices.length; x++) {
        for (y = 0; y < stockPrices.length; y++) {
          if (x < y && (stockPrices[y] - stockPrices[x] > bestProfit)) {
            bestProfit = stockPrices[y] - stockPrices[x];
            profitDays = [x, y];
          }
        }
      }
      return profitDays;
    }

    var pairs = stock_picker([44, 30, 24, 32, 35, 30, 40, 38]);
    console.log(pairs);
ready
b
    function stock_picker(stockPrices) {
      var profitDays = [0, 0];
      var low = 0;
      var high = 0;
      var bestPrice = 0;

      for (i = 0; i < stockPrices.length; i++) {
        if (stockPrices[i] < stockPrices[low]) {
          low = i;
          high = i;
        } else if (stockPrices[high] < stockPrices[i]) {
          high = i;
          if (stockPrice[i] - stockPrices[low] > bestPrice) {
            bestPrice = stockPrices[i] - stockPrices[low];
            profitDays = [low, high];
          }
        }
      }
      return profitDays;
    }

    var pairs = stock_picker([44, 30, 24, 32, 35, 30, 40, 38]);
    console.log(pairs);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Carroll Yu on