[Advanced Algorithm] - Inventory Update
题目
依照一个存着新进货物的二维数组,更新存着现有库存(在 arr1 中)的二维数组. 如果货物已存在则更新数量 . 如果没有对应货物则把其加入到数组中,更新最新的数量. 返回当前的库存数组,且按货物名称的字母顺序排列.
提示
测试用例
updateInventory()应该返回一个数组.updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]).length应该返回一个长度为6的数组.updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])应该返回[[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]].updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [])应该返回[[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]].updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])应该返回[[67, "Bowling Ball"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"]].updateInventory([[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]])应该返回[[1, "Bowling Ball"], [0, "Dirty Sock"], [1, "Hair Pin"], [1, "Half-Eaten Apple"], [0, "Microphone"], [1, "Toothpaste"]].
分析思路
- 遍历进货数组,取每一个元素,然后遍历库存数组,若进货的名称已存在,直接修改数目,若进货的名称不存在,这 push 到库存中;
- 从测试用例中可知:最终的库存列表需要根据名称进行排序,从字符的低到高进行排序;
代码
function updateInventory(arr1, arr2) {
// All inventory must be accounted for or you're fired!
for (var i = 0; i < arr2.length; i++) {
for (var j = 0; j < arr1.length; j++) {
if (arr1[j][1] === arr2[i][1]) {
arr1[j][0] += arr2[i][0];
break;
}
}
if (j == arr1.length) {
arr1.push (arr2[i]);
}
}
return arr1.sort(function(a, b) {
return a[1].charCodeAt(0) - b[1].charCodeAt(0);
});
}
// Example inventory lists
var curInv = [
[21, "Bowling Ball"],
[2, "Dirty Sock"],
[1, "Hair Pin"],
[5, "Microphone"]
];
var newInv = [
[2, "Hair Pin"],
[3, "Half-Eaten Apple"],
[67, "Bowling Ball"],
[7, "Toothpaste"]
];
updateInventory(curInv, newInv);
[Advanced Algorithm] - Inventory Update的更多相关文章
- FCC高级编程之Inventory Update
Inventory Update Compare and update the inventory stored in a 2D array against a second 2D array of ...
- FCC(ES6写法) Inventory Update
依照一个存着新进货物的二维数组,更新存着现有库存(在 arr1 中)的二维数组. 如果货物已存在则更新数量 . 如果没有对应货物则把其加入到数组中,更新最新的数量. 返回当前的库存数组,且按货物名称的 ...
- Inventory Update
依照一个存着新进货物的二维数组,更新存着现有库存(在 arr1 中)的二维数组. 如果货物已存在则更新数量 . 如果没有对应货物则把其加入到数组中,更新最新的数量. 返回当前的库存数组,且按货物名称的 ...
- 高级算法设计讲义 Lecture Notes for Advanced Algorithm Design
(Last modification: 2012-12-17) Textbooks: (1) David Williamson, David Shmoys. The Design of Approxi ...
- [Advanced Algorithm] - Exact Change
题目 设计一个收银程序 checkCashRegister(),其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数. ci ...
- [Advanced Algorithm] - Symmetric Difference
题目 创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or ⊕)数组. 给出两个集合 (如集合 A = {1, 2, 3}和集合 B = ...
- [Advanced Algorithm] - Validate US Telephone Numbers
题目 如果传入字符串是一个有效的美国电话号码,则返回 true. 用户可以在表单中填入一个任意有效美国电话号码. 下面是一些有效号码的例子(还有下面测试时用到的一些变体写法): 555-555-555 ...
- Inventory Costing in AX 2009
I wanted to explore some scenarios that illustrate a few important concepts related to inventory cos ...
- Inventory Update-freecodecamp算法题目
Inventory Update 1.要求 依照一个存着新进货物的二维数组,更新存着现有库存(在 arr1 中)的二维数组. 如果货物已存在则更新数量 . 如果没有对应货物则把其加入到数组中,更新最新 ...
随机推荐
- vue 瀑布流实现
<div class="myWrite" v-if="list.length==0"> - 这个福宝有点懒哦 - </div> < ...
- Python网络请求urllib和urllib3详解
Python网络请求urllib和urllib3详解 urllib是Python中请求url连接的官方标准库,在Python2中主要为urllib和urllib2,在Python3中整合成了urlli ...
- 【codeforces 757D】Felicity's Big Secret Revealed
[题目链接]:http://codeforces.com/problemset/problem/757/D [题意] 给你一个01串; 让你分割这个01串; 要求2切..n+1切; 对于每一种切法 所 ...
- python库文件路径
python中import语句导入库文件路径可通过sys.path查看.写一个简单的小程序: import sys print sys.path 运行它,本机上得到的结果如下: ['', '/usr/ ...
- HDU - 3556 - Continued Fraction
先上题目: Continued Fraction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Jav ...
- P1464 Function 洛谷
https://www.luogu.org/problem/show?pid=1464 题目描述 对于一个递归函数w(a,b,c) 如果a<=0 or b<=0 or c<=0就返回 ...
- 源代码:windows文件切割与合并
#include <Windows.h> #include <vector> #include <string> using namespace std; //推断 ...
- 我要抓狂了。。又回到了几天不能A一道题的时候
poj1556我不做了.你做做把...我已经要game over了
- selenium获取弹窗提示
1.点击保存给的提示是几秒钟,遮罩显示 2. 其他弹窗处理方法 http://blog.csdn.net/Real_Tino/article/details/59068827
- hihoCoder - 1079 - 离散化 (线段树 + 离散化)
#1079 : 离散化 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho在回国之后,又一次过起了朝7晚5的学生生活.当然了.他们还是在一直学习着各种算法 ...