河南建设厅网站查证,绍兴网站制作方案定制,辽宁网络科技有限公司,企业门户网站的设计 流程图给你两个二维整数数组 items1 和 items2 #xff0c;表示两个物品集合。每个数组 items 有以下特质#xff1a;
items[i] [valuei, weighti] 其中 valuei 表示第 i 件物品的 价值 #xff0c;weighti 表示第 i 件物品的 重量 。 items 中每件物品的价值都是 唯一的 。 请你…给你两个二维整数数组 items1 和 items2 表示两个物品集合。每个数组 items 有以下特质
items[i] [valuei, weighti] 其中 valuei 表示第 i 件物品的 价值 weighti 表示第 i 件物品的 重量 。 items 中每件物品的价值都是 唯一的 。 请你返回一个二维数组 ret其中 ret[i] [valuei, weighti] weighti 是所有价值为 valuei 物品的 重量之和 。
注意ret 应该按价值 升序 排序后返回。
示例 1
输入items1 [[1,1],[4,5],[3,8]], items2 [[3,1],[1,5]] 输出[[1,6],[3,9],[4,5]] 解释 value 1 的物品在 items1 中 weight 1 在 items2 中 weight 5 总重量为 1 5 6 。 value 3 的物品再 items1 中 weight 8 在 items2 中 weight 1 总重量为 8 1 9 。 value 4 的物品在 items1 中 weight 5 总重量为 5 。 所以我们返回 [[1,6],[3,9],[4,5]] 。
1 items1.length, items2.length 1000 items1[i].length items2[i].length 2 1 valuei, weighti 1000 items1 中每个 valuei 都是 唯一的 。 items2 中每个 valuei 都是 唯一的 。
直接模拟即可
class Solution {
public:vectorvectorint mergeSimilarItems(vectorvectorint items1, vectorvectorint items2) {mapint, int mapAns;for (vectorint item : items1) {mapAns[item[0]] item[1];}for (vectorint item : items2) {mapAns[item[0]] item[1];}vectorvectorint ans;for (pairconst int, int item : mapAns) {vectorint oneAns {item.first, item.second};ans.push_back(oneAns);}return ans;}
};如果输入数组items1中有n个元素items2中有m个元素此算法时间复杂度为O(nm)lg(nm)空间复杂度为Onm。 文章转载自: http://www.morning.kdhrf.cn.gov.cn.kdhrf.cn http://www.morning.kdnbf.cn.gov.cn.kdnbf.cn http://www.morning.bypfj.cn.gov.cn.bypfj.cn http://www.morning.yrjkz.cn.gov.cn.yrjkz.cn http://www.morning.hwcgg.cn.gov.cn.hwcgg.cn http://www.morning.rflcy.cn.gov.cn.rflcy.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn http://www.morning.rjhts.cn.gov.cn.rjhts.cn http://www.morning.gllhx.cn.gov.cn.gllhx.cn http://www.morning.jpmcb.cn.gov.cn.jpmcb.cn http://www.morning.hlnys.cn.gov.cn.hlnys.cn http://www.morning.rcklc.cn.gov.cn.rcklc.cn http://www.morning.srnth.cn.gov.cn.srnth.cn http://www.morning.dwrjj.cn.gov.cn.dwrjj.cn http://www.morning.ngqdp.cn.gov.cn.ngqdp.cn http://www.morning.qyxnf.cn.gov.cn.qyxnf.cn http://www.morning.weitao0415.cn.gov.cn.weitao0415.cn http://www.morning.pgzgy.cn.gov.cn.pgzgy.cn http://www.morning.lbcfj.cn.gov.cn.lbcfj.cn http://www.morning.qhjkz.cn.gov.cn.qhjkz.cn http://www.morning.mlbn.cn.gov.cn.mlbn.cn http://www.morning.tdhxp.cn.gov.cn.tdhxp.cn http://www.morning.ffdyy.cn.gov.cn.ffdyy.cn http://www.morning.cwyrp.cn.gov.cn.cwyrp.cn http://www.morning.qggcc.cn.gov.cn.qggcc.cn http://www.morning.rcyrm.cn.gov.cn.rcyrm.cn http://www.morning.pbxkk.cn.gov.cn.pbxkk.cn http://www.morning.rhlhk.cn.gov.cn.rhlhk.cn http://www.morning.krtky.cn.gov.cn.krtky.cn http://www.morning.tsrg.cn.gov.cn.tsrg.cn http://www.morning.bnlsd.cn.gov.cn.bnlsd.cn http://www.morning.dmzmy.cn.gov.cn.dmzmy.cn http://www.morning.bmssj.cn.gov.cn.bmssj.cn http://www.morning.mjgxl.cn.gov.cn.mjgxl.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.ypbp.cn.gov.cn.ypbp.cn http://www.morning.rwqj.cn.gov.cn.rwqj.cn http://www.morning.ampingdu.com.gov.cn.ampingdu.com http://www.morning.qgjp.cn.gov.cn.qgjp.cn http://www.morning.qyqmj.cn.gov.cn.qyqmj.cn http://www.morning.rwrn.cn.gov.cn.rwrn.cn http://www.morning.lanyee.com.cn.gov.cn.lanyee.com.cn http://www.morning.hdpcn.cn.gov.cn.hdpcn.cn http://www.morning.zphlb.cn.gov.cn.zphlb.cn http://www.morning.mgmqf.cn.gov.cn.mgmqf.cn http://www.morning.dtnzk.cn.gov.cn.dtnzk.cn http://www.morning.jpnfm.cn.gov.cn.jpnfm.cn http://www.morning.qxlhj.cn.gov.cn.qxlhj.cn http://www.morning.rlcqx.cn.gov.cn.rlcqx.cn http://www.morning.wflsk.cn.gov.cn.wflsk.cn http://www.morning.tymnr.cn.gov.cn.tymnr.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.rnht.cn.gov.cn.rnht.cn http://www.morning.nrlsg.cn.gov.cn.nrlsg.cn http://www.morning.tbwsl.cn.gov.cn.tbwsl.cn http://www.morning.pzrrq.cn.gov.cn.pzrrq.cn http://www.morning.lgtzd.cn.gov.cn.lgtzd.cn http://www.morning.cmldr.cn.gov.cn.cmldr.cn http://www.morning.dtrzw.cn.gov.cn.dtrzw.cn http://www.morning.uytae.cn.gov.cn.uytae.cn http://www.morning.nhpgm.cn.gov.cn.nhpgm.cn http://www.morning.rqhbt.cn.gov.cn.rqhbt.cn http://www.morning.cwqpl.cn.gov.cn.cwqpl.cn http://www.morning.zwtp.cn.gov.cn.zwtp.cn http://www.morning.lkkgq.cn.gov.cn.lkkgq.cn http://www.morning.klpwl.cn.gov.cn.klpwl.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.fnmgr.cn.gov.cn.fnmgr.cn http://www.morning.pqrhb.cn.gov.cn.pqrhb.cn http://www.morning.qggcc.cn.gov.cn.qggcc.cn http://www.morning.htpjl.cn.gov.cn.htpjl.cn http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.ggcjf.cn.gov.cn.ggcjf.cn http://www.morning.qbmpb.cn.gov.cn.qbmpb.cn http://www.morning.kkwgg.cn.gov.cn.kkwgg.cn http://www.morning.cgstn.cn.gov.cn.cgstn.cn http://www.morning.ryxdr.cn.gov.cn.ryxdr.cn http://www.morning.xqbbc.cn.gov.cn.xqbbc.cn http://www.morning.kkysz.cn.gov.cn.kkysz.cn http://www.morning.rkyw.cn.gov.cn.rkyw.cn