给定一个数组arr[] ,任务是打印大于其左侧所有元素以及大于其右侧下一个 K 元素的元素数。
例子:
Input: arr[] = { 4, 2, 3, 6, 4, 3, 2}, K = 2
Output: 2
Explanation:
arr[0](= 4): arr[0] is the 1st element in the array and greater than its next K(= 2) elements {2, 3}.
arr[2](= 6): arr[2] is greater than all elements on its left {4, 2, 3} and greater than its next K(= 2) elements {4, 3}.
Therefore, only two elements satisfy the given condition.
Input: arr[] = { 3, 1, 2, 7, 5, 1, 2, 6}, K = 2
Output: 2
天真的方法:
遍历数组,对于每个元素,检查它左边的所有元素是否都小于它,以及它右边的下一个 K 元素是否小于它。对于每个这样的元素,增加count 。最后,打印count 。
时间复杂度: O(N 2 )
辅助空间: O(1)
有效的方法:
上述方法可以通过使用堆栈数据结构进行优化。请按照以下步骤解决问题:
- 初始化一个新数组并使用Stack存储每个数组元素的下一个大元素的索引。
- 遍历给定的数组,对于每个元素,检查它是否是迄今为止获得的最大值,并且其下一个更大的元素至少是当前索引之后的K 个索引。如果发现是真的,增加计数。
- 最后,打印count 。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to print the count of
// Array elements greater than all
// elements on its left and next K
// elements on its right
int countElements(int arr[], int n,
int k)
{
stack s;
vector next_greater(n, n + 1);
// Iterate over the array
for (int i = 0; i < n; i++) {
if (s.empty()) {
s.push(i);
continue;
}
// If the stack is not empty and
// the element at the top of the
// stack is smaller than arr[i]
while (!s.empty()
&& arr[s.top()] < arr[i]) {
// Store the index of next
// greater element
next_greater[s.top()] = i;
// Pop the top element
s.pop();
}
// Insert the current index
s.push(i);
}
// Stores the count
int count = 0;
int maxi = INT_MIN;
for (int i = 0; i < n; i++) {
if (next_greater[i] - i > k
&& maxi < arr[i]) {
maxi = max(maxi, arr[i]);
count++;
}
}
return count;
}
// Driver Code
int main()
{
int arr[] = { 4, 2, 3, 6, 4, 3, 2 };
int K = 2;
int n = sizeof(arr) / sizeof(arr[0]);
cout << countElements(arr, n, K);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to print the count of
// Array elements greater than all
// elements on its left and next K
// elements on its right
static int countElements(int arr[], int n,
int k)
{
Stack s = new Stack();
int []next_greater = new int[n + 1];
Arrays.fill(next_greater, n);
// Iterate over the array
for(int i = 0; i < n; i++)
{
if (s.isEmpty())
{
s.add(i);
continue;
}
// If the stack is not empty and
// the element at the top of the
// stack is smaller than arr[i]
while (!s.isEmpty() &&
arr[s.peek()] < arr[i])
{
// Store the index of next
// greater element
next_greater[s.peek()] = i;
// Pop the top element
s.pop();
}
// Insert the current index
s.add(i);
}
// Stores the count
int count = 0;
int maxi = Integer.MIN_VALUE;
for(int i = 0; i < n; i++)
{
if (next_greater[i] - i > k &&
maxi < arr[i])
{
maxi = Math.max(maxi, arr[i]);
count++;
}
}
return count;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 4, 2, 3, 6, 4, 3, 2 };
int K = 2;
int n = arr.length;
System.out.print(countElements(arr, n, K));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to implement
# the above approach
import sys
# Function to print the count of
# Array elements greater than all
# elements on its left and next K
# elements on its right
def countElements(arr, n, k):
s = []
next_greater = [n] * (n + 1)
# Iterate over the array
for i in range(n):
if(len(s) == 0):
s.append(i)
continue
# If the stack is not empty and
# the element at the top of the
# stack is smaller than arr[i]
while(len(s) != 0 and
arr[s[-1]] < arr[i]):
# Store the index of next
# greater element
next_greater[s[-1]] = i
# Pop the top element
s.pop(-1)
# Insert the current index
s.append(i)
# Stores the count
count = 0
maxi = -sys.maxsize - 1
for i in range(n):
if(next_greater[i] - i > k and
maxi < arr[i]):
maxi = max(maxi, arr[i])
count += 1
return count
# Driver Code
if __name__ == '__main__':
arr = [ 4, 2, 3, 6, 4, 3, 2 ]
K = 2
n = len(arr)
# Function call
print(countElements(arr, n, K))
# This code is contributed by Shivam Singh
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to print the count of
// Array elements greater than all
// elements on its left and next K
// elements on its right
static int countElements(int[] arr, int n,
int k)
{
Stack s = new Stack();
int[] next_greater = new int[n + 1];
Array.Fill(next_greater, n);
// Iterate over the array
for(int i = 0; i < n; i++)
{
if (s.Count == 0)
{
s.Push(i);
continue;
}
// If the stack is not empty and
// the element at the top of the
// stack is smaller than arr[i]
while (s.Count != 0 &&
arr[s.Peek()] < arr[i])
{
// Store the index of next
// greater element
next_greater[s.Peek()] = i;
// Pop the top element
s.Pop();
}
// Insert the current index
s.Push(i);
}
// Stores the count
int count = 0;
int maxi = Int32.MinValue;
for(int i = 0; i < n; i++)
{
if (next_greater[i] - i > k &&
maxi < arr[i])
{
maxi = Math.Max(maxi, arr[i]);
count++;
}
}
return count;
}
// Driver Code
static void Main()
{
int[] arr = { 4, 2, 3, 6, 4, 3, 2 };
int K = 2;
int n = arr.Length;
Console.Write(countElements(arr, n, K));
}
}
// This code is contributed by divyeshrabadiya07
Javascript
2
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live