Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 26e6f11

Browse files
Minimum Index Sum of Two Lists
1 parent 17d7202 commit 26e6f11

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
""" https://leetcode.com/problems/minimum-index-sum-of-two-lists/ """
2+
3+
class Solution:
4+
def findRestaurant(self, list1, list2):
5+
commons = []
6+
list1Indexes = []
7+
list2Indexes = []
8+
9+
for str in list1:
10+
if str in list2:
11+
commons.append(str)
12+
list1Indexes.append(list1.index(str))
13+
list2Indexes.append(list2.index(str))
14+
15+
if (len(commons) <= 1):
16+
return commons
17+
else:
18+
leastCommons = []
19+
least = list1Indexes[0] + list2Indexes[0]
20+
leastCommons.append(commons[0])
21+
22+
for i in range(1, len(commons)):
23+
sum = list1Indexes[i] + list2Indexes[i]
24+
25+
if (sum < least):
26+
least = sum
27+
leastCommons.clear()
28+
leastCommons.append(commons[i])
29+
elif (sum == least):
30+
least = sum
31+
leastCommons.append(commons[i])
32+
33+
return leastCommons
34+
35+
print(Solution().findRestaurant(["happy","sad","good"], ["sad","happy","good"]))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /