-
Notifications
You must be signed in to change notification settings - Fork 0
/
599_MinimumIndexSumOfTwoLists.js
51 lines (44 loc) · 1.37 KB
/
599_MinimumIndexSumOfTwoLists.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// https://leetcode.com/problems/minimum-index-sum-of-two-lists/#/description
/**
* @param {string[]} list1
* @param {string[]} list2
* @return {string[]}
*/
var findRestaurant = function (list1, list2) {
var list1Len = list1.length;
var list2Len = list2.length;
if (list1Len > list2Len) {
var temp = list1;
list1 = list2;
list2 = temp;
}
var find = [];
var sum;
for (var i = 0; i < list1.length; i++) {
var item = list1[i];
var index = list2.indexOf(item);
if (index === -1) continue;
if (sum === undefined || sum > i + index) {
find = [item];
sum = i + index;
} else if (sum === (i + index)) {
find.push(item);
}
}
return find;
};
var list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"];
var list2 = ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"];
console.log(findRestaurant(list1, list2));
list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"];
list2 = ["KFC", "Shogun", "Burger King"];
console.log(findRestaurant(list1, list2));
list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"];
list2 = ["KFC", "Burger King", "Tapioca Express", "Shogun"];
console.log(findRestaurant(list1, list2));
list1 = ["k", "KFC"];
list2 = ["k", "KFC"];
console.log(findRestaurant(list1, list2));
list1 = ["nfeau", "KFC"];
list2 = ["KFC"];
console.log(findRestaurant(list1, list2));