当前位置:网站首页>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)
边栏推荐
- 【Rust 笔记】08-枚举与模式
- Golang 时间格式整理
- Creation and content of mapnode -- osgearth rendering engine series (2)
- Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
- Unity editor expansion - window, sub window, menu, right-click menu (context menu)
- [concurrent programming] working mechanism and type of thread pool
- Abstract classes and interfaces
- Display terrain database on osgearth ball
- Development material set
- UE4 source code reading_ Bone model and animation system_ Animation compression
猜你喜欢

Vscode, idea, VIM development tool shortcut keys

Introduction to Base64 coding

Installation of PHP FPM software +openresty cache construction

Mall management system of database application technology course design

Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template

数据分析练习题
![[concurrent programming] concurrent tool class of thread](/img/16/2b4d2b3528b138304a1a3918773ecf.jpg)
[concurrent programming] concurrent tool class of thread

Gradle's method of dynamically modifying APK package name
![P1596 [USACO10OCT]Lake Counting S](/img/a7/07a84c93ee476788d9443c0add808b.png)
P1596 [USACO10OCT]Lake Counting S

php-fpm软件的安装+openresty高速缓存搭建
随机推荐
C#课程设计之员工信息管理系统
php-fpm软件的安装+openresty高速缓存搭建
Visual Studio (VS) shortcut keys
Golang string segmentation, substitution and interception
详解sizeof、strlen、指针和数组等组合题
Introduction to hexadecimal coding
100 GIS practical application cases (78) - Multi compliance database design and data warehousing
Base64 and base64url
MySQL 8
[concurrent programming] synchronization container, concurrent container, blocking queue, double ended queue and work secret
UE4 source code reading_ Bone model and animation system_ Animation process
【Rust 笔记】12-闭包
Redis的数据结构
Intersectionpicker in osgearth
Unity Editor Extension - drag and drop
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
Introduction to Base64 coding
[updating] wechat applet learning notes_ three
【Rust笔记】06-包和模块
了解小程序的笔记 2022/7/3