In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Minimum stack
Implement a minimum stack, optimizing step by step, from extra space O (N) to O (1). The interviewer values code logic. Push,pop,top,getMin is all O (1) time.
1 use a minimum stack to store the minimum value 1.1 points:
2 stacks, data to store data and minValue to store minimum values.
When push, data directly push data; minValue puts directly into the current minimum value. There is an optimization for minValue, when the data of push is larger than the current minimum value, we can not insert the minimum value of minValue; if it is less than or equal to the minimum value, we need to put the latest minimum value push into the stack minValue.
When pop, data directly pop the data; at the same time, update minValue, and the updated policy is the number of pop corresponding to the optimization in push. If = = the current minimum value, you need to pop the minValue once.
GetMin: just return the top element of the stack minValue directly.
Top: just return the top element of the stack data directly.
1.2 complexity and code
Extra space consumption O (N), how to optimize to O (1).
Public class MinStack1 {private Stack data = new Stack (); private Stack minValue = new Stack (); public void push (int x) {data.push (x); if (minValue.isEmpty () | | x 0? Top + minValue: minValue;} public int getMin () {return minValue;}}
New channel IELTS
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.