Skip to content

Commit 099822a

Browse files
solves minimum index of 2 lists
1 parent d19f8da commit 099822a

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
| 590 | [N-Ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal) | Easy | [![Java](assets/java.png)](src/NAryTreePostorderTraversal.java) [![Python](assets/python.png)](python/n_ary_tree_postorder_traversal.py) |
158158
| 594 | [Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence) | Easy | [![Java](assets/java.png)](src/LongestHarmoniousSubsequence.java) [![Python](assets/python.png)](python/longest_harmonious_subequence.py) |
159159
| 598 | [Range Addition II](https://leetcode.com/problems/range-addition-ii) | Easy | [![Java](assets/java.png)](src/RangeAdditionII.java) [![Python](assets/python.png)](python/range_addition_ii.py) |
160-
| 599 | [Minimum Index Sum of 2 Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists) | Easy | |
160+
| 599 | [Minimum Index Sum of 2 Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists) | Easy | [![Java](assets/java.png)](src/MinimumIndexSumOfTwoLists.java) [![Python](assets/python.png)](python/minimum_index_sum_of_two_lists.py) |
161161
| 604 | [Design Compressed String Iterator](https://leetcode.com/problems/design-compressed-string-iterator) | Easy | |
162162
| 605 | [Can Place Flowers](https://leetcode.com/problems/can-place-flowers) | Easy | |
163163
| 606 | [Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree) | Easy | |
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
6+
restaurant1_2_indices = {}
7+
index_sum = float('inf')
8+
result = []
9+
for index, restaurant in enumerate(list1):
10+
restaurant1_2_indices[restaurant] = index
11+
for index, restaurant in enumerate(list2):
12+
if index + restaurant1_2_indices.get(restaurant, float('inf')) < index_sum:
13+
index_sum = index + restaurant1_2_indices[restaurant]
14+
result = [restaurant]
15+
elif index + restaurant1_2_indices.get(restaurant, float('inf')) == index_sum:
16+
result.append(restaurant)
17+
return result

src/MinimumIndexSumOfTwoLists.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
public class MinimumIndexSumOfTwoLists {
7+
public String[] findRestaurant(String[] list1, String[] list2) {
8+
int indexSum = Integer.MAX_VALUE;
9+
List<String> result = new ArrayList<>();
10+
Map<String, Integer> restaurantIndices = getValueToIndexMap(list1);
11+
for (int index = 0 ; index < list2.length ; index++) {
12+
if (restaurantIndices.containsKey(list2[index])) {
13+
if (index + restaurantIndices.get(list2[index]) < indexSum) {
14+
result.clear();
15+
result.add(list2[index]);
16+
indexSum = index + restaurantIndices.get(list2[index]);
17+
} else if(index + restaurantIndices.get(list2[index]) == indexSum) {
18+
result.add(list2[index]);
19+
}
20+
}
21+
}
22+
return toArray(result);
23+
}
24+
25+
private String[] toArray(List<String> strings) {
26+
String[] result = new String[strings.size()];
27+
for (int index = 0 ; index < result.length ; index++) {
28+
result[index] = strings.get(index);
29+
}
30+
return result;
31+
}
32+
33+
private Map<String, Integer> getValueToIndexMap(String[] array) {
34+
Map<String, Integer> result = new HashMap<>();
35+
for (int index = 0 ; index < array.length ; index++) {
36+
result.put(array[index], index);
37+
}
38+
return result;
39+
}
40+
}

0 commit comments

Comments
 (0)