File tree 5 files changed +81
-0
lines changed
algorithms/MinimumIndexSumOfTwoLists 5 files changed +81
-0
lines changed Original file line number Diff line number Diff line change @@ -88,6 +88,7 @@ All solutions will be accepted!
88
88
| 563| [ Binary Tree Tilt] ( https://leetcode-cn.com/problems/binary-tree-tilt/description/ ) | [ java/py/js] ( ./algorithms/BinaryTreeTilt ) | Easy|
89
89
| 696| [ Count Binary Substrings] ( https://leetcode-cn.com/problems/count-binary-substrings/description/ ) | [ java/py/js] ( ./algorithms/CountBinarySubstrings ) | Easy|
90
90
| 121| [ Best Time To Buy And Sell Stock] ( https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/ ) | [ java/py/js] ( ./algorithms/BestTimeToBuyAndSellStock ) | Easy|
91
+ | 599| [ Minimum Index Sum Of Two Lists] ( https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/description/ ) | [ java/py/js] ( ./algorithms/MinimumIndexSumOfTwoLists ) | Easy|
91
92
92
93
# Database
93
94
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Minimum Index Sum Of Two Lists
2
+ This problem is easy to solve by hashmap
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public String [] findRestaurant (String [] list1 , String [] list2 ) {
3
+ List <String > res = new ArrayList <String >();
4
+ Map <String , Integer > indexMap = new HashMap <String , Integer >();
5
+ int minIndexSum = Integer .MAX_VALUE ;
6
+
7
+ for (int i = 0 ; i < list1 .length ; i ++) {
8
+ indexMap .put (list1 [i ], i );
9
+ }
10
+
11
+ for (int i = 0 ; i < list2 .length ; i ++) {
12
+ if (indexMap .get (list2 [i ]) != null ) {
13
+ int indexSum = indexMap .get (list2 [i ]) + i ;
14
+
15
+ if (indexSum < minIndexSum ) {
16
+ res .clear ();
17
+ res .add (list2 [i ]);
18
+ minIndexSum = indexSum ;
19
+ } else if (indexSum == minIndexSum ) {
20
+ res .add (list2 [i ]);
21
+ }
22
+ }
23
+ }
24
+
25
+ return res .toArray (new String [res .size ()]);
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string[] } list1
3
+ * @param {string[] } list2
4
+ * @return {string[] }
5
+ */
6
+ var findRestaurant = function ( list1 , list2 ) {
7
+ let res = [ ] ,
8
+ resturantMap = { } ,
9
+ minIndexSum = Number . MAX_VALUE
10
+
11
+ for ( let i = 0 ; i < list1 . length ; i ++ ) {
12
+ resturantMap [ list1 [ i ] ] = i
13
+ }
14
+
15
+ for ( let i = 0 ; i < list2 . length ; i ++ ) {
16
+ if ( resturantMap [ list2 [ i ] ] !== undefined ) {
17
+ let indexSum = resturantMap [ list2 [ i ] ] + i
18
+ if ( indexSum < minIndexSum ) {
19
+ res = [ list2 [ i ] ]
20
+ minIndexSum = indexSum
21
+ } else if ( indexSum === minIndexSum ) {
22
+ res . push ( list2 [ i ] )
23
+ }
24
+ }
25
+ }
26
+
27
+ return res
28
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def findRestaurant (self , list1 , list2 ):
3
+ """
4
+ :type list1: List[str]
5
+ :type list2: List[str]
6
+ :rtype: List[str]
7
+ """
8
+ res = []
9
+ resturant_map = {}
10
+ min_index_sum = sys .maxint
11
+ for i in range (0 , len (list1 )):
12
+ resturant_map [list1 [i ]] = i
13
+
14
+ for i in range (0 , len (list2 )):
15
+ if resturant_map .get (list2 [i ]) != None :
16
+ index_sum = resturant_map .get (list2 [i ]) + i
17
+ if index_sum < min_index_sum :
18
+ res = [list2 [i ]]
19
+ min_index_sum = index_sum
20
+ elif index_sum == min_index_sum :
21
+ res .append (list2 [i ])
22
+
23
+ return res
You can’t perform that action at this time.
0 commit comments