Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, output -1 for this number.

Example 1:

Input: [1,2,1]
Output: [2,-1,2]
Explanation: The first 1's next greater number is 2;
The number 2 can't find next greater number;
The second 1's next greater number needs to search circularly, which is also 2.

Note: The length of given array won't exceed 10000.

496. Next Greater Element I 的拓展,这里的数组是循环的,某一个元素的下一个较大值可以在其前面。

解法:栈,与496的不同是:循环是2倍的数组长度,用栈来保存降序序列的index。

Java:

public int[] nextGreaterElements(int[] nums) {
int n = nums.length, next[] = new int[n];
Arrays.fill(next, -1);
Stack<Integer> stack = new Stack<>(); // index stack
for (int i = 0; i < n * 2; i++) {
int num = nums[i % n];
while (!stack.isEmpty() && nums[stack.peek()] < num)
next[stack.pop()] = num;
if (i < n) stack.push(i);
}
return next;
}

Python:

def nextGreaterElements(self, nums):
stack, res = [], [-1] * len(nums)
for i in range(len(nums)) * 2:
while stack and (nums[stack[-1]] < nums[i]):
res[stack.pop()] = nums[i]
stack.append(i)
return res 

C++:

vector<int> nextGreaterElements(vector<int>& nums) {
int n = nums.size();
vector<int> next(n, -1);
stack<int> s; // index stack
for (int i = 0; i < n * 2; i++) {
int num = nums[i % n];
while (!s.empty() && nums[s.top()] < num) {
next[s.top()] = num;
s.pop();
}
if (i < n) s.push(i);
}
return next;
} 

  

类似题目:

[LeetCode] 496. Next Greater Element I 下一个较大的元素 I

[LeetCode] 556. Next Greater Element III 下一个较大的元素 III

All LeetCode Questions List 题目汇总

[LeetCode] 503. Next Greater Element II 下一个较大的元素 II的更多相关文章

  1. [LeetCode] 496. Next Greater Element I 下一个较大的元素 I

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  2. [LeetCode] 556. Next Greater Element III 下一个较大的元素 III

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  3. Leetcode496.Next Greater Element I下一个更大的元素1

    给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更 ...

  4. [leetcode]496. Next Greater Element I下一个较大元素

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  5. [LeetCode] Next Greater Element II 下一个较大的元素之二

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  6. 496 Next Greater Element I 下一个更大元素 I

    给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值.nums1 中数字 x 的下一个更大 ...

  7. [LeetCode] Next Greater Element III 下一个较大的元素之三

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  8. [LeetCode] Next Greater Element I 下一个较大的元素之一

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  9. 556. Next Greater Element III下一个更大的数字

    [抄题]: Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exac ...

随机推荐

  1. LoadRunner 11的破解方法和license号

    安装过程中遇到“命令行选项语法错误键入命令 \?获得帮助”2005的安装问题,可参考本文:http://www.cnblogs.com/lelexiong/p/8974149.html解决 破解方法: ...

  2. 微信小程序底部导航栏部署

    在微信小程序开发app.json(app.json它是定义全局页面) 只是用来部署微信底部的图标,最多不能大于五个 "tabBar":{ "selectedColor&q ...

  3. php之大文件分段上传、断点续传

    前段时间做视频上传业务,通过网页上传视频到服务器. 视频大小 小则几十M,大则 1G+,以一般的HTTP请求发送数据的方式的话,会遇到的问题:1,文件过大,超出服务端的请求大小限制:2,请求时间过长, ...

  4. BZOJ 5082: 弗拉格 矩阵乘法

    如果单点而不是求 sigma 的话还是比较好办的. 遇到这种前缀和相减的矩阵乘法可以增设一个 0 使得后面的能先加到前面,然后再算. 这样的话可以使的最后算出的是前缀和相加的形式. code: #in ...

  5. 关于System.BadImageFormatException

    什么是BadImageFormatException BadImageFormatException是当动态链接库 (DLL) 或可执行程序的文件映像无效时引发的异常. 可能的原因 如果动态链接库 ( ...

  6. python 报can't subtract offset-naive and offset-aware datetimes错误

    两个时间一个含时区,一个不含时区

  7. javascript之反柯里化uncurrying

    使用方法: // 使用 var push=Array.prototype.push.uncurrying(); var obj={ "length": 1, "0&quo ...

  8. mysql cos() 函数

    mysql> ); +--------------------+ | cos(PI()/) | +--------------------+ | 0.5000000000000001 | +-- ...

  9. E4A写的app,点按钮,直接进入抖音指定用户界面

    今天在网上看到有一个人,直接进抖音某个指定用户的界面,一般模拟的方式,要先通过搜索的方式,再选用户,点进去 但是这样操作,不大友好,也影响速度 最理想的方式,是通过 "无障碍",直接控制抖音进入指定的 ...

  10. linux桌面系统开启windows远程访问

    本文基于centos系统,且默认你的桌面系统已经安装完成 1.在linux中安装如下软件 sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epe ...