Skip to content

Commit 750b1e5

Browse files
committed
add minimum-index-sum-of-two-lists script.
1 parent 0f6a73f commit 750b1e5

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

599-minimum-index-sum-of-two-lists.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {string[]} list1
3+
* @param {string[]} list2
4+
* @return {string[]}
5+
*/
6+
const findRestaurant = function(list1, list2) {
7+
const hash = {};
8+
for (let i = 0; i < list1.length; i++) {
9+
if (!hash.hasOwnProperty(list1[i])) {
10+
hash[list1[i]] = i;
11+
}
12+
}
13+
const resArr = [];
14+
for (let j = 0; j < list2.length; j++) {
15+
if (hash.hasOwnProperty(list2[j])) {
16+
resArr.push([list2[j], hash[list2[j]] + j]);
17+
}
18+
}
19+
const resHash = {};
20+
resArr.forEach(el => {
21+
if (resHash.hasOwnProperty(el[1])) {
22+
resHash[el[1]].push(el[0]);
23+
} else {
24+
resHash[el[1]] = [el[0]];
25+
}
26+
});
27+
resArr.sort((a, b) => a[1] - b[1]);
28+
return resHash[resArr[0][1]];
29+
};
30+
31+
console.log(
32+
findRestaurant(
33+
["Shogun", "Tapioca Express", "Burger King", "KFC"],
34+
["KFC", "Burger King", "Tapioca Express", "Shogun"]
35+
)
36+
);

0 commit comments

Comments
 (0)