给定一个大小为N的数组arr[] ,任务是找到以arr[i]结尾的子数组的数量,而arr[i]是该子数组的最小元素。
例子:
Input: arr[] = {3, 1, 2, 4}
Output: 1 2 1 1
Explanation:
Subarrays ending with 3 where 3 is the minimum element = {3}
Subarrays ending with 1 where 1 is the minimum element = {3, 1}, {1}
Subarrays ending with 2 where 2 is the minimum element = {2}
Subarrays ending with 4 where 4 is the minimum element = {4}
Input: arr[] = {5, 4, 3, 2, 1}
Output: 1 2 3 4 5
方法:这个想法是使用用于通过维护堆栈来查找下一个更大元素的方法。问题的分步方法是:
- 将计数为 1 的数组的第一个元素 (arr[0]) 压入堆栈,因为第一个元素将是子数组本身,以当前元素 arr[0] 和子数组的最小值结尾
- 然后对于数组中的每个元素 arr[i]-
- 从栈中弹出元素,直到栈顶大于当前元素,并将弹出元素的计数加到当前元素的计数中。
- 将当前元素和计数作为一对推送到堆栈中。
例如:对于 arr[] = {3, 1, 2, 4},
下面是上述方法的实现:
C++
// C++ implementation to find the number
// of sub-arrays ending with arr[i] which
// is the minimum element of the subarray
#include
using namespace std;
// Function to find the number
// of sub-arrays ending with arr[i] which
// is the minimum element of the subarray
int min_subarray(int a[], int n)
{
stack > st;
for (int i = 0; i < n; ++i) {
// There exists a subarray of
// size 1 for each element
int count = 1;
// Remove all greater elements
while (!st.empty() &&
st.top().first > a[i]) {
// Increment the count
count += st.top().second;
// Remove the element
st.pop();
}
// Push the current element
// and it's count
st.push({ a[i], count });
cout << count << " ";
}
}
// Driver Code
int main()
{
int a[] = {5, 4, 3, 2, 1};
int n = sizeof(a) / sizeof(a[0]);
min_subarray(a, n);
return 0;
}
Java
// Java implementation to find the number
// of sub-arrays ending with arr[i] which
// is the minimum element of the subarray
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
static class Pair
{
int first;
int second;
public Pair(int x, int y)
{
this.first = x;
this.second = y;
}
}
// Function to find the number
// of sub-arrays ending with arr[i] which
// is the minimum element of the subarray
static void min_subarray(int []a, int n)
{
Stack st = new Stack();
for (int i = 0; i < n; ++i)
{
// There exists a subarray of
// size 1 for each element
int count = 1;
// Remove all greater elements
while (st.empty() == false &&
st.peek().first > a[i])
{
// Increment the count
count += st.peek().second;
// Remove the element
st.pop();
}
// Push the current element
// and it's count
st.push(new Pair (a[i], count ));
System.out.print(count + " ");
}
}
// Driver Code
public static void main(String []args)
{
int []a = {5, 4, 3, 2, 1};
int n = a.length;
min_subarray(a, n);
}
}
// This code is contributed by tufan_gupta2000
Python3
# Python3 implementation to find the number
# of sub-arrays ending with arr[i] which
# is the minimum element of the subarray
# Function to find the number
# of sub-arrays ending with arr[i] which
# is the minimum element of the subarray
def min_subarray(a, n) :
st = [];
for i in range(n) :
# There exists a subarray of
# size 1 for each element
count = 1;
# Remove all greater elements
while len(st) != 0 and st[-1][0] > a[i] :
# Increment the count
count += st[-1][1];
# Remove the element
st.pop();
# Push the current element
# and it's count
st.append(( a[i], count ));
print(count,end= " ");
# Driver Code
if __name__ == "__main__" :
a = [5, 4, 3, 2, 1];
n = len(a);
min_subarray(a, n);
# This code is contributed by AnkitRai01
C#
// C# implementation to find the number
// of sub-arrays ending with arr[i] which
// is the minimum element of the subarray
using System;
using System.Collections.Generic;
class GFG
{
class Pair
{
public int first;
public int second;
public Pair(int x, int y)
{
this.first = x;
this.second = y;
}
}
// Function to find the number
// of sub-arrays ending with arr[i] which
// is the minimum element of the subarray
static void min_subarray(int []a, int n)
{
Stack st = new Stack();
for (int i = 0; i < n; ++i)
{
// There exists a subarray of
// size 1 for each element
int count = 1;
// Remove all greater elements
while (st.Count != 0 &&
st.Peek().first > a[i])
{
// Increment the count
count += st.Peek().second;
// Remove the element
st.Pop();
}
// Push the current element
// and it's count
st.Push(new Pair (a[i], count ));
Console.Write(count + " ");
}
}
// Driver Code
public static void Main(String []args)
{
int []a = {5, 4, 3, 2, 1};
int n = a.Length;
min_subarray(a, n);
}
}
// This code is contributed by Rajput-Ji
输出:
1 2 3 4 5
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live