Skip to content

Commit 997eb5c

Browse files
committed
Add solution #599
1 parent 33da287 commit 997eb5c

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
565|[Array Nesting](./0565-array-nesting.js)|Medium|
124124
566|[Reshape the Matrix](./0566-reshape-the-matrix.js)|Easy|
125125
567|[Permutation in String](./0567-permutation-in-string.js)|Medium|
126+
599|[Minimum Index Sum of Two Lists](./0599-minimum-index-sum-of-two-lists.js)|Easy|
126127
606|[Construct String from Binary Tree](./0606-construct-string-from-binary-tree.js)|Easy|
127128
617|[Merge Two Binary Trees](./0617-merge-two-binary-trees.js)|Easy|
128129
628|[Maximum Product of Three Numbers](./0628-maximum-product-of-three-numbers.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 599. Minimum Index Sum of Two Lists
3+
* https://leetcode.com/problems/minimum-index-sum-of-two-lists/
4+
* Difficulty: Easy
5+
*
6+
* Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list
7+
* of favorite restaurants represented by strings.
8+
*
9+
* You need to help them find out their common interest with the least list index sum.
10+
* If there is a choice tie between answers, output all of them with no order requirement.
11+
* You could assume there always exists an answer.
12+
*/
13+
14+
/**
15+
* @param {string[]} list1
16+
* @param {string[]} list2
17+
* @return {string[]}
18+
*/
19+
var findRestaurant = function(list1, list2) {
20+
const map = new Map(list1.map((str, index) => [str, index]));
21+
22+
return list2
23+
.map((str, index) => map.has(str) ? [map.get(str) + index, str] : null)
24+
.filter(Boolean)
25+
.sort(([a], [b]) => a - b)
26+
.filter(([sum], index, [[lowest]]) => sum === lowest)
27+
.map(([, str]) => str);
28+
};

0 commit comments

Comments
 (0)