当前位置:网站首页>Monotonic stack -503 Next bigger Element II
Monotonic stack -503 Next bigger Element II
2022-07-03 08:35:00 【later_ rql】
Monotonic stack -503. Next bigger element II
1. Their thinking
First , Determine the solution idea of this problem , The title requires to record the value greater than this number on the right of each number , The essence is to find the nearest value larger than the current number to the right , And the title explains that the array is a circular array .
Decide to use monotone stack to solve this problem :
Monotonous stack usage scenarios : It's usually a one-dimensional array , To find the position of the first element larger or smaller than yourself on the right or left of any element , At this point, we have to think of using monotone stack .
2. difficulty
This problem is a little more difficult , Is to turn the array into a circular array , You only need to take the remainder .
3. Code
public int[] nextGreaterElements(int[] nums) {
int len=nums.length;
int[] result=new int[nums.length];
for (int i=0;i<result.length;i++){
result[i]=-1;
}
Stack<Integer> stack = new Stack<>();
stack.push(0);
for (int i=1;i<len*2;i++){
while (!stack.isEmpty() && nums[(i%len)]>nums[stack.peek()]){
if (result[stack.peek()]==-1){
result[stack.peek()]=nums[i%len];
}
stack.pop();
}
stack.push(i%len);
}
return result;
}
4. performance
- Time complexity :O(n)
- Spatial complexity :O(n)
边栏推荐
- MySQL containerization (1) docker installation MySQL
- Visual Studio (VS) shortcut keys
- UE4 source code reading_ Bone model and animation system_ Animation node
- [RPC] RPC remote procedure call
- 【Rust 笔记】10-操作符重载
- P1596 [USACO10OCT]Lake Counting S
- Unity notes 1
- GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
- UE4 source code reading_ Mobile synchronization
- Osgconv tool usage
猜你喜欢
Base64和Base64URL
【更新中】微信小程序学习笔记_3
Notes on understanding applets 2022/7/3
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
Kunlunbase meetup is waiting for you!
Unity learning notes
详解sizeof、strlen、指针和数组等组合题
单调栈-42. 接雨水
Clion toolchains are not configured configure disable profile problem solving
Base64 and base64url
随机推荐
【Rust 笔记】07-结构体
Transmit pictures with Base64 encoding
Location of package cache downloaded by unity packagemanager
Huawei interview summary during the epidemic
Display terrain database on osgearth ball
Cesium service deployment, and import and display local 3dfiles data
UE4 plug in development
Simply start with the essence and principle of SOM neural network
【Rust笔记】02-所有权
Downward compatibility and upward compatibility
Mall management system of database application technology course design
Installation of PHP FPM software +openresty cache construction
producer consumer problem
Osgearth target selection
Unity multi open script
Unity Editor Extension - drag and drop
Development material set
Introduction to Base64 coding
P1596 [USACO10OCT]Lake Counting S
String class