javascript - 对于这个数据结构/算法问题,我应该如何改变我的方法?
问题描述
我正在练习数据结构和算法,但遇到了一个困扰我的问题。
您有一个输入数组,其中包含格式为“产品、销售量、价格”的元素。您需要返回一个数组,其中包含按销售量排序的所有产品。如果两种产品的销售量相同,则按最低价格对它们进行排序。
我开始的方式是:
- 循环遍历数组,用逗号分割元素
- 将产品名称和价格添加到以销量为关键的产品对象中
- 跟踪最大销售量
- 从最大值开始,到零,如果有具有该键的产品,将名称和价格推送到我的返回数组
- 返回数组
这至少给了我按销售量排序的产品。但是当我有销售相同数量的产品时,它就不起作用了。如果物品的销售量相同,我不确定按价格对它们进行分类的有效方法。
任何人都知道如何实现价格排序?或者解决这个问题的更好方法?
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; }
解决方案
...我上述评论中的文字变成了代码...
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 ...从上述函数的最后几行可以看出,原来的方法已经指向正确的方向......
- 将每个产品项的字符串模板形式映射到一个真实的、基于键值的产品项(也保留字符串模板)。
sold
通过以正确的顺序 ( ,price
, )将相应的属性值相互比较来对临时项数组进行排序name
。- 通过映射回
template
属性来恢复每个产品项的原始形式。
推荐阅读
- spring-boot - IllegalArgumentException:在 HTTP 协议中发现无效字符
- android - 使用 ML Kit 进行带计数的深蹲姿势检测
- r - 决策树回归器中的超参数
- sql-server - “ORDER BY”子句与“INNER JOIN On MAX with GROUP BY”
- shell - 如何在 shell 脚本中存储在变量中的每个单词周围添加引号
- python - 将数据添加到 txt 文件(并将其保存在那里)
- oracle-apex - SSO 回调配置
- python - 在 if-else 块中使用 try-except
- android - 安卓。从服务启动 Activity
- perl - 将从文本文件读取的行存储到 Perl 脚本中的变量