Skip to content

Commit 645c290

Browse files
committed
solve problem Minimum Index Sum Of Two Lists
1 parent ad0876a commit 645c290

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ All solutions will be accepted!
8888
|563|[Binary Tree Tilt](https://leetcode-cn.com/problems/binary-tree-tilt/description/)|[java/py/js](./algorithms/BinaryTreeTilt)|Easy|
8989
|696|[Count Binary Substrings](https://leetcode-cn.com/problems/count-binary-substrings/description/)|[java/py/js](./algorithms/CountBinarySubstrings)|Easy|
9090
|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|
9192

9293
# Database
9394
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Minimum Index Sum Of Two Lists
2+
This problem is easy to solve by hashmap
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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

0 commit comments

Comments
 (0)