【问题标题】:What should I change about my approach to this data structure/algorithm problem?对于这个数据结构/算法问题,我应该如何改变我的方法?
【发布时间】:2020-12-07 16:04:58
【问题描述】:

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

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

我开始的方式是:

  • 遍历数组,用逗号分割元素
  • 将产品名称和价格添加到以销量为关键的产品对象中
  • 跟踪最大销售量
  • 从最大值开始,到零,如果有具有该键的产品,将名称和价格推送到我的返回数组中
  • 返回数组

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

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

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; }

【问题讨论】:

  • 你已经拥有了一切。它就在你面前。方法是正确的。只需将每个产品的字符串版本映射到包含所有产品数据的项目。然后通过sort 和一个自定义比较函数对这个产品项目数组进行排序,首先比较amount sold,然后(如果相等)比较price,最后(如果再次相等)按姓名

标签: javascript arrays algorithm sorting data-structures


【解决方案1】:

...我的above comment 中的文字具体化为代码...

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. 通过按正确顺序(soldpricename)将相应的属性值相互比较来对临时项数组进行排序。
  3. 通过映射回template 属性来恢复每个产品项的原始形式。

【讨论】:

    猜你喜欢
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多