当前位置:网站首页>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)$
边栏推荐
- How to install an application publisher
- How to register a trademark? How to improve the passing rate of trademark registration?
- Hungry? Remote dual live database practice
- Where is the domain name filed? What materials are required for domain name filing?
- Create and mount large files
- Coding -- the leader of R & D tools in the cloud native Era
- How to handle the abnormal state of easycvr national standard cascading superior display?
- Afnetworking usage and cache processing
- Tornado code for file download
- Question: can you get download the resources of Baidu online disk?
猜你喜欢
随机推荐
Grpc: adjust data transfer size limit
How to use nsfilemanager
Contour-v1.19.1 release
Activiti obtains the initiator based on the process instance ID
Data backup is required for manual upgrade of WordPress website program
How to bind EIP to access public network in tke cluster fixed IP mode pod
Create and mount large files
Go language starts again, go modules' past life, present life and basic use
Coding -- the leader of R & D tools in the cloud native Era
In PHP, use recursive depth to merge multiple arrays
Internal reasons for cloud desktop unable to connect to the server and external reasons for cloud desktop connection failure
Build your own cloud game server. What if the cloud game server is attacked
Mysql Find_ IN_ Set function
Is your database "cloud native"?
5g Gigabit router dual band industrial grade
How to quickly handle third-party login and easy to expand?
Hook principle
Vscode common shortcut keys, updating
Gin framework: add Prometheus monitoring
What about registered domain names? How long does it take to register a domain name?

