Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does leetcode solve the next larger element problem

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how leetcode to solve the next bigger element problem, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

I. the content of the topic

Given a circular array (the next element of the last element is the first element of the array), output the next larger element of each element. The next larger element of the number x is in the order of traversing the array, and the first larger number after this number means that you should cycle through its next larger number. If it does not exist, output-1.

Example 1:

Input: [1Jing 2jue 1]

Output: [2mai Mei 1JI 2]

Explanation: the next larger number of the first 1 is 2.

The number 2 cannot find the next larger number.

The next maximum number of the second 1 requires a circular search, and the result is also 2.

Note: the length of the input array will not exceed 10000.

Second, the way to solve the problem

Nums the list twice, then res stores a larger element under each number, and s stores the subscript of each element

The current element (the next nums element corresponding to the top of the stack in s) is larger than the nums element corresponding to the top of the stack in s, the next larger element of the top subscript of the s stack corresponding to res is the current element, and the top subscript of the s pop-up stack

S stores only the subscript of the nums length, not the array subscript after the second splicing.

Code class Solution: def nextGreaterElements (self Nums: list)-> list: res = [- 1 for _ in range (len (nums))] s = [] two_nums = nums + nums for i in range (len (two_nums)): while s and nums [s [- 1]] < two_nums [I]: res [s [- 1]] = two_ nums [I] # current element ( The next nums element corresponding to the top of stack in s) is greater than the corresponding nums element s.pop () # pop in s. If the subscript # I is greater than the length of nums, the subscript I < len (nums): s.append (I) return resif _ _ name__ = ='_ _ main__': is not added to s. S = Solution () nums = [1 2, 1] ans = s.nextGreaterElements (nums) print (ans) these are all the contents of the article "how leetcode solves the next larger element problem" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report