-
Notifications
You must be signed in to change notification settings - Fork 6
/
MinimumIndexSumOfTwoLists.java
49 lines (47 loc) · 1.43 KB
/
MinimumIndexSumOfTwoLists.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package io.ziheng.hashtable.leetcode;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.LinkedList;
/**
* LeetCode 599. Minimum Index Sum of Two Lists
* https://leetcode.com/problems/minimum-index-sum-of-two-lists/
*/
public class MinimumIndexSumOfTwoLists {
/**
* Time Complexity: O(n + m)
* Space Complexity: O(n)
*
* @param list1
* @param list2
* @return String[]
*/
public String[] findRestaurant(String[] list1, String[] list2) {
if (list1 == null
|| list1.length == 0
|| list2 == null
|| list2.length == 0) {
return new String[0];
}
List<String> resultList = new LinkedList<>();
int minIndexSum = Integer.MAX_VALUE;
Map<String, Integer> aMap = new HashMap<>();
for (int i = 0; i < list1.length; i++) {
aMap.put(list1[i], i);
}
for (int i = 0; i < list2.length; i++) {
if (aMap.containsKey(list2[i])) {
int j = aMap.get(list2[i]);
if (i + j < minIndexSum) {
resultList.clear();
resultList.add(list2[i]);
minIndexSum = i + j;
} else if (i + j == minIndexSum) {
resultList.add(list2[i]);
}
}
}
return resultList.toArray(new String[resultList.size()]);
}
}
/* EOF */