Algorithm
Problem Name: 503. Next Greater Element II
Given a circular integer array nums
(i.e., the next element of nums[nums.length - 1]
is nums[0]
), return the next greater number for every element in nums
.
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, return -1
for this number.
Example 1:
Input: nums = [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.
Example 2:
Input: nums = [1,2,3,4,3] Output: [2,3,4,-1,4]
Constraints:
1 <= nums.length <= 104
-109 <= nums[i] <= 109
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
int n = nums.size();
vector<int>res;
for(int i = 0; i < nums.size(); i++){
int j = (i + 1) % n;
while(j != i){
if(nums[j] > nums[i]) break;
++j %= n;
}
res.push_back(j == i ? -1 : nums[j]);
}
return res;
}
};
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Java Programming
Code -
Java Programming
class Solution {
public int[] nextGreaterElements(int[] nums) {
int[] result = new int[nums.length];
Stack < Integer> stack = new Stack<>();
for (int i = nums.length - 1; i >= 0; i--) {
while (!stack.isEmpty() && nums[stack.peek()] < = nums[i]) {
stack.pop();
}
result[i] = stack.isEmpty() ? -1 : nums[stack.peek()];
stack.push(i);
}
for (int i = nums.length - 1; i >= 0; i--) {
if (result[i] == -1) {
while (!stack.isEmpty() && nums[stack.peek()] < = nums[i]) {
stack.pop();
}
result[i] = stack.isEmpty() ? -1 : nums[stack.peek()];
}
stack.push(i);
}
return result;
}
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
const nextGreaterElements = function(nums) {
const res = [];
for (let i = 0; i < nums.length; i++) {
res.push(single(i, nums));
}
return res;
};
function single(idx, arr) {
const base = arr[idx];
const prev = idx === 0 ? [] : arr.slice(0, idx);
const next = arr.slice(idx);
const comb = next.concat(prev);
for (let i = 0; i < comb.length; i++) {
if (comb[i] > base) return comb[i];
}
return -1;
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
class Solution:
def nextGreaterElements(self, nums):
stack, res = [], [-1] * len(nums)
for j in range(2):
for i in range(len(nums)):
while stack and (nums[stack[-1]] < nums[i]): res[stack.pop()] = nums[i]
if j == 1 and not stack: break
stack += i,
return res
Copy The Code &
Try With Live Editor
Input
Output
#5 Code Example with C# Programming
Code -
C# Programming
using System.Collections.Generic;
namespace LeetCode
{
public class _0503_NextGreaterElementII
{
public int[] NextGreaterElements(int[] nums)
{
var stack = new Stack < int>();
var n = nums.Length;
var result = new int[n];
for (int i = 0; i < n; i++)
result[i] = -1;
for (int i = 0; i < n * 2; i++)
{
while (stack.Count > 0 && nums[stack.Peek()] < nums[i % n])
result[stack.Pop()] = nums[i % n];
if (i < n)
stack.Push(i);
}
return result;
}
}
}
Copy The Code &
Try With Live Editor
Input
Output