Skip to main content

Leetcode - 599. Minimum Index Sum of Two Lists

· One min read

https://leetcode.com/problems/minimum-index-sum-of-two-lists/

/**
* @param {string[]} list1
* @param {string[]} list2
* @return {string[]}
*/
var findRestaurant = function (list1, list2) {
const hashTable = {};

list1.forEach((el, index) => {
const indexOfEl2 = list2.findIndex((el2) => el2 === el);
if (indexOfEl2 !== -1) {
hashTable[el] = indexOfEl2 + index;
}
});

const size = Object.keys(hashTable).length;
if (size === 0) {
return [];
}

const minIndexSum = Math.min(
...Object.values(hashTable).map((value) => value),
);

return Object.keys(hashTable).filter((key) => hashTable[key] === minIndexSum);
};

Runtime: 309 ms, faster than 13.28% of JavaScript online submissions for Minimum Index Sum of Two Lists.

Memory Usage: 49.2 MB, less than 54.39% of JavaScript online submissions for Minimum Index Sum of Two Lists.