首页 > 解决方案 > 对于这个数据结构/算法问题,我应该如何改变我的方法?

问题描述

我正在练习数据结构和算法,但遇到了一个困扰我的问题。

您有一个输入数组,其中包含格式为“产品、销售量、价格”的元素。您需要返回一个数组,其中包含按销售量排序的所有产品。如果两种产品的销售量相同,则按最低价格对它们进行排序。

我开始的方式是:

这至少给了我按销售量排序的产品。但是当我有销售相同数量的产品时,它就不起作用了。如果物品的销售量相同,我不确定按价格对它们进行分类的有效方法。

任何人都知道如何实现价格排序?或者解决这个问题的更好方法?

const items = [
  'Chair, 100, 20',
  'Sofa, 70, 200',
  'Desk, 80, 120',
  'Table, 400, 300',
  'Fan, 10, 60',
  'Pillow, 40, 5',
  'Blanket, 40, 20',
  'Rug, 100, 200',
  'Mat, 2, 30',
  'Stool, 80, 40',
  'Comforter, 200, 250',
  'Recliner, 50, 350',
];

class Product {
  constructor(name, price) {
    this.name = name;
    this.price = price;
  }
}
const productList = (items) => {
  let returnArray = [];
  let products = {};
  let max = 0;
  for (let i = 0; i < items.length; i++) {
    let productItems = items[i].split(',');
    let product = new Product(productItems[0].trim(), productItems[2].trim());
    let orderAmt = parseInt(productItems[1].trim());

    products[orderAmt] = product;

    max = Math.max(max, productItems[1]);
  }

  while (max > 0) {
    if (products[max]) {
      returnArray.push(`${products[max].name}, ${max}, ${products[max].price}`);
    }
    max--;
  }
  return returnArray;
};

console.log(productList(items));
.as-console-wrapper { min-height: 100%!important; top: 0; }

标签: javascriptarraysalgorithmsortingdata-structures

解决方案


...我上述评论中的文字变成了代码...

const productItemList = [
  'Chair, 100, 20',
  'Sofa, 70, 200',
  'Desk, 80, 120',
  'Table, 400, 300',
  'Fan, 10, 60',
  'Pillow, 40, 5',
  'Blanket, 40, 20',
  'Rug, 100, 200',
  'Mat, 2, 30',
  'Stool, 80, 40',
  'Comforter, 200, 250',
  'Recliner, 50, 350',
];

function getProductItemListAlignedBySoldAmountPriceAndName(itemList) {
  function compareAlphaNumericalAscending(a, b) {
    return (((a < b) && -1) || ((a > b) && 1) || 0);
  }
  function compareAlphaNumericalDescending(a, b) {
    return (((a > b) && -1) || ((a < b) && 1) || 0);
  }
  function compareNames(a, b) {
    return (
      (a.localeCompare && b.localeCompare)
      ? a.localeCompare(b)
      : compareAlphaNumericalAscending(a, b)
    );
  }

  function compareProductItemsByAmountSoldPriceAndName(a, b) {
    return (
         compareAlphaNumericalDescending(a.sold, b.sold)
      || compareAlphaNumericalAscending(a.price, b.price)
      || compareNames(a.name, b.name)
    );
  }
  function createProductItem(template) {
    const [name, sold, price] = template.split(/\s*,\s*/);
    return {
      name,
      sold: parseInt(sold, 10),
      price: parseInt(price, 10),
      template
    };
  }
  function getProductItemTemplate(item) {
    return item.template;
  }

  return itemList
    .map(createProductItem)
    .sort(compareProductItemsByAmountSoldPriceAndName)
    .map(getProductItemTemplate);
}

console.log(getProductItemListAlignedBySoldAmountPriceAndName(productItemList));
.as-console-wrapper { min-height: 100%!important; top: 0; }

@Cineno28 ...从上述函数的最后几行可以看出,原来的方法已经指向正确的方向......

  1. 将每个产品项的字符串模板形式映射到一个真实的、基于键值的产品项(也保留字符串模板)。
  2. sold通过以正确的顺序 ( , price, )将相应的属性值相互比较来对临时项数组进行排序name
  3. 通过映射回template属性来恢复每个产品项的原始形式。

推荐阅读