给定一个由N 个整数组成的循环数组arr[] ,任务是为循环数组的每个元素打印下一个更大的元素。不存在更大元素的元素,打印“-1” 。
例子:
Input: arr[] = {5, 6, 7}
Output: 6 7 -1
Explanation: The next greater element for every array element are as follows:
For arr[0] (= 5) -> 6
For arr[1] (= 6) -> 7
For arr[2] (= 7), no greater element is present in the array. Therefore, print -1.
Input: arr[] = {4, -2, 5, 3}
Output: 5 5 -1 4
Explanation: The next greater element for every array element are as follows:
For arr[0] (= 4) -> 5
For arr[1] (= -2) -> 5
For arr[2] (= 5), no greater element is present in the array. Therefore, print -1.
For arr[3] (= 3) -> 4
朴素的方法:本文的前一篇文章讨论了解决这个问题的最简单的方法。
时间复杂度: O(N 2 )
辅助空间: O(N)
替代方法:请参阅本文的前一篇文章,了解朴素方法的空间优化。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:为了优化上述方法,其思想是使用使用堆栈数据结构的下一个更大元素的概念。请按照以下步骤解决问题:
- 初始化一个堆栈来存储数组的索引和一个大小为N的数组nge[] ,它存储每个数组元素的下一个更大的元素。
- 遍历数组并对每个索引执行以下操作:
- 如果栈非空且当前第i个数组元素大于栈顶元素,则弹出栈顶元素并通过存储arr[i % N]更新nge[st.top() ]在里面。重复此操作,直到堆栈为空或当前元素小于堆栈顶部的元素。
- 如果堆栈为空或当前元素小于堆栈顶部,则将(i % N)压入堆栈。
- 在单次遍历N 个元素之后,堆栈包含在第(N – 1)个索引之前没有下一个更大元素的元素。由于数组是圆形的,再次考虑第0个索引中的所有元素,并找到剩余元素的下一个更大的元素。
- 由于数组遍历了2 次,因此最好使用i % N而不是i 。
- 完成上述步骤后,打印数组nge[] 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the NGE for the
// given circular array arr[]
void printNGE(int* arr, int n)
{
// Initialize stack and nge[] array
stack s;
int nge[n], i = 0;
// Initialize nge[] array to -1
for (i = 0; i < n; i++) {
nge[i] = -1;
}
i = 0;
// Traverse the array
while (i < 2 * n) {
// If stack is not empty and
// current element is greater
// than top element of the stack
while (!s.empty()
&& arr[i % n] > arr[s.top()]) {
// Assign next greater element
// for the top element of the stack
nge[s.top()] = arr[i % n];
// Pop the top element
// of the stack
s.pop();
}
s.push(i % n);
i++;
}
// Print the nge[] array
for (i = 0; i < n; i++) {
cout << nge[i] << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 4, -2, 5, 8 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
printNGE(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the NGE for the
// given circular array arr[]
static void printNGE(int arr[], int n)
{
// Initialize stack and nge[] array
Stack s = new Stack<>();
int nge[] = new int[n];
int i = 0;
// Initialize nge[] array to -1
for(i = 0; i < n; i++)
{
nge[i] = -1;
}
i = 0;
// Traverse the array
while (i < 2 * n)
{
// If stack is not empty and
// current element is greater
// than top element of the stack
while (!s.isEmpty() &&
arr[i % n] > arr[s.peek()])
{
// Assign next greater element
// for the top element of the stack
nge[s.peek()] = arr[i % n];
// Pop the top element
// of the stack
s.pop();
}
s.push(i % n);
i++;
}
// Print the nge[] array
for(i = 0; i < n; i++)
{
System.out.print(nge[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 4, -2, 5, 8 };
int N = arr.length;
// Function Call
printNGE(arr, N);
}
}
// This code is contributed by yashbeersingh42
Python3
# Python3 program for the above approach
# Function to find the NGE for the
# given circular array arr[]
def printNGE(arr, n) :
# create stack list
s = [];
# Initialize nge[] array to -1
nge = [-1] * n;
i = 0;
# Traverse the array
while (i < 2 * n) :
# If stack is not empty and
# current element is greater
# than top element of the stack
while (len(s) != 0 and arr[i % n] > arr[s[-1]]) :
# Assign next greater element
# for the top element of the stack
nge[s[-1]] = arr[i % n];
# Pop the top element
# of the stack
s.pop();
s.append(i % n);
i += 1;
# Print the nge[] array
for i in range(n) :
print(nge[i], end= " ");
# Driver Code
if __name__ == "__main__" :
arr = [ 4, -2, 5, 8 ];
N = len(arr);
# Function Call
printNGE(arr, N);
# This code is contributed by AnkitRai01
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the NGE for the
// given circular array []arr
static void printNGE(int []arr, int n)
{
// Initialize stack and nge[] array
Stack s = new Stack();
int []nge = new int[n];
int i = 0;
// Initialize nge[] array to -1
for(i = 0; i < n; i++)
{
nge[i] = -1;
}
i = 0;
// Traverse the array
while (i < 2 * n)
{
// If stack is not empty and
// current element is greater
// than top element of the stack
while (s.Count != 0 &&
arr[i % n] > arr[s.Peek()])
{
// Assign next greater element
// for the top element of the stack
nge[s.Peek()] = arr[i % n];
// Pop the top element
// of the stack
s.Pop();
}
s.Push(i % n);
i++;
}
// Print the nge[] array
for(i = 0; i < n; i++)
{
Console.Write(nge[i] + " ");
}
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 4, -2, 5, 8 };
int N = arr.Length;
// Function Call
printNGE(arr, N);
}
}
// This code is contributed by 29AjayKumar
输出:
5 5 8 -1
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live