File tree 2 files changed +29
-0
lines changed
2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 123
123
565|[ Array Nesting] ( ./0565-array-nesting.js ) |Medium|
124
124
566|[ Reshape the Matrix] ( ./0566-reshape-the-matrix.js ) |Easy|
125
125
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|
126
127
606|[ Construct String from Binary Tree] ( ./0606-construct-string-from-binary-tree.js ) |Easy|
127
128
617|[ Merge Two Binary Trees] ( ./0617-merge-two-binary-trees.js ) |Easy|
128
129
628|[ Maximum Product of Three Numbers] ( ./0628-maximum-product-of-three-numbers.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments