当前位置:网站首页>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 batch output ean 13 code to pictures
- [security] Tencent cloud double 12 is not limited to new and old users! Safe sub venue - discount guide! Unlimited account types!
- 5g Gigabit router dual band industrial grade
- [Tencent cloud double 12.12] from 56 yuan! New users of Tencent cloud buy for the first time, which is more cost-effective!
- How to access easynvr management platform through web pages without data?
- Mutual conversion between qstring and qdatetime
- How to improve the success rate of trademark registration? How long does it take to register a trademark?
- More than 10 million Android users installed a fraud app and doubled the number of blackmail attacks in the UK | global network security hotspot
- Build a reliable, scalable and maintainable application system
- Do you still understand the deadlock handling methods in MySQL performance testing and tuning?
猜你喜欢
随机推荐
Cloud rendering: cloud exhibition hall of Tencent digital ecology Conference - open roaming mode on cloud
The cloud game is rendered by the server. How much broadband does the server need
Start tcapulusdb process
What information should be provided for enterprise trademark registration? Is it difficult to register a trademark?
MySQL Cases-MySQL 8.0.26 bug ERROR 1064 (42000) at line1: You have an error
What about foreign trade companies? Is this another difficult year?
How to understand EDI requirements of trading partners
What are the main functions of DNS? What are the benefits of IP address translation
How to access easynvr management platform through web pages without data?
Official spoilers! Figure 1 understand Tencent security @2021 Tencent digital ecology Conference
SAP retail characteristic profile I
How to build a speech synthesis server
Implementing an ORM framework against SQL injection with builder mode
How to bind EIP to access public network in tke cluster fixed IP mode pod
How to improve the success rate of trademark registration? How long does it take to register a trademark?
The whole procurement process of the procurement mall website in the transportation industry is electronic to reduce the procurement cost
Gin framework: add Prometheus monitoring
Build your own cloud game server. What if the cloud game server is attacked
Coding -- the leader of R & D tools in the cloud native Era
PHP verify mailbox format

