当前位置:网站首页>LeetCode 599. Minimum index sum of two lists
LeetCode 599. Minimum index sum of two lists
2022-06-24 02:41:00 【freesan44】
Title address (599. The minimum index sum of two lists )
https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/
Title Description
hypothesis Andy and Doris Want to choose a restaurant for dinner , And they all have a list of their favorite restaurants , The name of each restaurant is represented by a string . You need to help them use the least index and find their favorite restaurants . If there is more than one answer , Then all the answers are output regardless of the order . You can assume that there is always an answer . Example 1: Input : ["Shogun", "Tapioca Express", "Burger King", "KFC"] ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"] Output : ["Shogun"] explain : Their only favorite restaurant is “Shogun”. Example 2: Input : ["Shogun", "Tapioca Express", "Burger King", "KFC"] ["KFC", "Shogun", "Burger King"] Output : ["Shogun"] explain : Their favorite restaurant with the smallest index and is “Shogun”, It has the smallest index and 1(0+1). Tips : The length range of both lists is [1, 1000] Inside . The length of the strings in the two lists will be in [1,30] Within the scope of . Subscript from 0 Start , Reduce the length of the list by 1. Neither list has duplicate elements .
Ideas
Save routing weight with hash table , Then traverse the value
Code
- Language support :Python3
Python3 Code:
class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
resDict = dict()
resList = list()
for index,val in enumerate(list1):
resDict[val] = index
for index,val in enumerate(list2):
if val in resDict:
resList.append((index+resDict[val],val))
# Yes index Sum sort , Get the value of the lowest weight , Then extract the same value
resList.sort(key=lambda x:x[0])
res = []
resIndex = resList[0][0]
for indexCount,val in resList:
if indexCount == resIndex:
res.append(val)
return res
if __name__ == '__main__':
list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"]
list2 = ["KFC", "Shogun", "Burger King"]
ret = Solution().findRestaurant(list1,list2)
print(ret)Complexity analysis
Make n Is array length .
- Time complexity :$O(n)$
- Spatial complexity :$O(n)$
边栏推荐
- What are the performance characteristics of cloud desktop? How to choose the most cost-effective cloud desktop server?
- VNC enters the password and goes around for a long time before entering the desktop. Use procmon to locate the reason
- How to use nsfilemanager
- Gartner released the magic quadrant of enterprise low code platform in 2021. Low code integrated platform becomes a trend!
- Grpc: how to enable tls/ssl?
- In PHP, use recursive depth to merge multiple arrays
- Uipickerview show and hide animation
- The cloud game is rendered by the server. How much broadband does the server need
- Build a reliable, scalable and maintainable application system
- The dealer management and control platform in the leather industry simplifies the purchase approval process and easily controls agents
猜你喜欢
随机推荐
Simple use of notification
Afnetworking server client
Interesting talk about decorator mode, so you will never forget it
Precautions for VPN client on Tencent cloud
How to change the cloud desktop domain server password if you forget it?
Is a trademark domain name useful? How long does it take to register a domain name?
Easycvr cannot be played when cascaded to the superior platform. Troubleshooting
How to build a cloud game server what needs to be considered to build a cloud game server
Grpc: adjust data transfer size limit
PHP verify mailbox format
Internal reasons for cloud desktop unable to connect to the server and external reasons for cloud desktop connection failure
Efficient Internet access and systematic learning
The dealer management and control platform in the leather industry simplifies the purchase approval process and easily controls agents
Buddha's foot before examination: the second play of leetcode
Can cloud computing scale flexibly? What are the characteristics of elasticity?
The reason why SAS fortress cannot connect to the server
Offline store + online mall, why do you want to be an online mall
The technical route is based on UE4 for secondary development
Some tips for using uitextview
What are the conditions for trademark registration? How long does it take to register a trademark?

