Skip to content

Commit 1d00fe2

Browse files
Minimum Index Sum of Two Lists: Accepted
1 parent 6e64553 commit 1d00fe2

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ My accepted leetcode solutions to some of the common interview problems.
3737
- [Largest Number At Least Twice of Others](problems/src/array/LargestNumberAtLeastTwice.java) (Easy)
3838
- [Minimum Moves to Equal Array Elements II](problems/src/array/MinimumMovesToEqualArray.java) (Median)
3939
- [Image Smoother](problems/src/array/ImageSmoother.java) (Easy)
40+
- [Minimum Index Sum of Two Lists](problems/src/array/MinimumIndexSumOfTwoLists.java) (Easy)
4041

4142
#### [Backtracking](problems/src/backtracking)
4243

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package array;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* Created by gouthamvidyapradhan on 11/04/2018.
10+
* Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants
11+
* represented by strings.
12+
13+
You need to help them find out their common interest with the least list index sum. If there is a choice tie between
14+
answers, output all of them with no order requirement. You could assume there always exists an answer.
15+
16+
Example 1:
17+
Input:
18+
["Shogun", "Tapioca Express", "Burger King", "KFC"]
19+
["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
20+
Output: ["Shogun"]
21+
Explanation: The only restaurant they both like is "Shogun".
22+
Example 2:
23+
Input:
24+
["Shogun", "Tapioca Express", "Burger King", "KFC"]
25+
["KFC", "Shogun", "Burger King"]
26+
Output: ["Shogun"]
27+
Explanation: The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1).
28+
Note:
29+
The length of both lists will be in the range of [1, 1000].
30+
The length of strings in both lists will be in the range of [1, 30].
31+
The index is starting from 0 to the list length minus 1.
32+
No duplicates in both lists.
33+
34+
Solution:O(N) maintain index of each restaurant in a list using a HashMap, find the min sum of indices and list
35+
all the restaurants which match the min sum of indices
36+
37+
*/
38+
public class MinimumIndexSumOfTwoLists {
39+
/**
40+
* Main method
41+
* @param args
42+
*/
43+
public static void main(String[] args) throws Exception{
44+
String[] A1 = {"Shogun", "Tapioca Express", "Burger King", "KFC"};
45+
String[] A2 = {"Tapioca Express", "Shogun", "Burger King"};
46+
String[] ans = new MinimumIndexSumOfTwoLists().findRestaurant(A1, A2);
47+
for(String s : ans){
48+
System.out.println(s);
49+
}
50+
}
51+
52+
public String[] findRestaurant(String[] list1, String[] list2) {
53+
int min = Integer.MAX_VALUE;
54+
List<String> result = new ArrayList<>();
55+
if(list2.length == 0) return new String[0];
56+
Map<String, Integer> index = new HashMap<>();
57+
for(int i = 0; i < list2.length; i ++){
58+
index.put(list2[i], i);
59+
}
60+
for(int i = 0; i < list1.length; i ++){
61+
String s = list1[i];
62+
if(index.containsKey(s)){
63+
int sum = i + index.get(s);
64+
min = Math.min(min, sum);
65+
}
66+
}
67+
68+
for(int i = 0; i < list1.length; i ++){
69+
String s = list1[i];
70+
if(index.containsKey(s)){
71+
int sum = i + index.get(s);
72+
if(sum == min){
73+
result.add(s);
74+
}
75+
}
76+
}
77+
String[] resArr = new String[result.size()];
78+
result.toArray(resArr);
79+
return resArr;
80+
}
81+
}

0 commit comments

Comments
 (0)