[LeetCode&Python] Problem 599. Minimum Index Sum of Two Lists
2024-10-18 00:05:34
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.
You need to help them find out their common interest with the least list index sum. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.
Example 1:
Input:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
Output: ["Shogun"]
Explanation: The only restaurant they both like is "Shogun".
Example 2:
Input:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["KFC", "Shogun", "Burger King"]
Output: ["Shogun"]
Explanation: The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1).
Note:
- The length of both lists will be in the range of [1, 1000].
- The length of strings in both lists will be in the range of [1, 30].
- The index is starting from 0 to the list length minus 1.
- No duplicates in both lists.
class Solution(object):
def findRestaurant(self, list1, list2):
"""
:type list1: List[str]
:type list2: List[str]
:rtype: List[str]
"""
ans=[]
ma=100000
n=len(list1)
for i in range(n):
s=list1[i]
if s in list2:
j=list2.index(s)
if i+j<=ma:
ma=i+j
ans.append(s)
return ans
最新文章
- 云服务器 Centos7.0 部署
- FastJson的使用
- aspxshell下突破无可写可执行目录执行cmd
- Sublime Text3 常用快捷键
- Bootstrap3.0学习第十一轮(输入框组)
- 通过硬件层提高Android动画的性能
- double类型如何保留2为小数
- IntelliJ IDEA javaDoc的使用
- Go 语言编写单元测试
- springboot~lombok使用总结
- 论学习IT的基本学习方法
- 洛谷P1048采药题解
- shell脚本--权限分配
- 合并两个sorted ranges(merge和inplace_merge)
- idea本地将本地现有的项目和gitlab进行管理并提交到线上
- C#提高--------------获取方法返回值的自定义特性(Attribute)
- 什么叫做hack
- Java字符串池
- [可行]setoolkit生成木马软件远程控制实例
- Qt事件系统基本概念