给定一个数组arr[] ,任务是找到后缀子数组的分解值之和。
分解值:子数组的分解值是子数组中可能的分区数。数组中索引处的分区只有当数组的元素小于当前索引时才可以完成。即 A[k] < A[i],其中 k ≤ i。
例子:
Input: arr[] = {2, 8, 4}
Output: 4
Explanation:
All suffixes subarray of arr[] are [2, 8, 4], [8, 4], [4]
Suffix [4] => only 1 decomposition {4}
Suffix [8, 4] => only 1 decomposition {8, 4}
Suffix [2, 8, 4] => 2 decompositions {2, 8, 4}, {2} {8, 4}
Hence, Sum of Decomposition values = 1 + 1 + 2 = 4
Input: arr[] = {9, 6, 9, 35}
Output: 8
Explanation:
All suffixes of arr are [9, 6, 9, 35], [6, 9, 35], [9, 35], [35]
Suffix [35] => only 1 decomposition {35}
Suffix [9, 35] => 2 decompositions {9} {35}
Suffix [6, 9, 35] => 3 decompositions {6} {9, 35}
Suffix [9, 6, 9, 35] => 2 decompositions {9, 6, 9} {35}
Hence, Sum of Decomposition values = 1 + 2 + 3 + 2 = 8
做法:思路是用Stack来解决这个问题。下面是该方法的说明
- 从头到尾遍历数组。
- 保持最小变量和答案变量。
- 如果堆栈为空或当前元素小于堆栈顶部 –
- 将 S[i] 压入堆栈。
- 按堆栈的大小增加答案。
- 另外,保持最小值直到现在。
- 除此以外,
- 只要栈顶小于当前元素,就继续弹出块。
- 用当前元素更新到现在的最小值。
- 将最小值压入堆栈。因为,我们希望子数组的最小值来表示该子数组
- 按堆栈的大小增加答案。
下面是上述方法的实现:
C++
// C++ implementation to find the
// sum of Decomposition values of
// all suffixes of an array
#include
using namespace std;
#define int long long int
// Function to find the decomposition
// values of the array
int decompose(vector S)
{
// Stack
stack s;
int N = S.size();
int ans = 0;
// Variable to maintain
// min value in stack
int nix = INT_MAX;
// Loop to iterate over the array
for (int i = N - 1; i >= 0; i--) {
// Condition to check if the
// stack is empty
if (s.empty()) {
s.push(S[i]);
nix = S[i];
}
else {
// Condition to check if the
// top of the stack is greater
// than the current element
if (S[i] < s.top()) {
s.push(S[i]);
nix = min(nix, S[i]);
}
else {
int val = S[i];
// Loop to pop the element out
while (!s.empty() &&
val >= s.top()) {
s.pop();
}
nix = min(nix, S[i]);
s.push(nix);
}
}
// the size of the stack is the
// max no of subarrays for
// suffix till index i
// from the right
ans += s.size();
}
return ans;
}
// Driver Code
signed main()
{
vector S = { 9, 6, 9, 35 };
cout << decompose(S) << endl;
return 0;
}
Java
// Java implementation to find the
// sum of Decomposition values of
// all suffixes of an array
import java.util.*;
class GFG{
// Function to find the decomposition
// values of the array
static int decompose(Vector S)
{
// Stack
Stack s = new Stack();
int N = S.size();
int ans = 0;
// Variable to maintain
// min value in stack
int nix = Integer.MAX_VALUE;
// Loop to iterate over the array
for(int i = N - 1; i >= 0; i--)
{
// Condition to check if the
// stack is empty
if (s.isEmpty())
{
s.add(S.get(i));
nix = S.get(i);
}
else
{
// Condition to check if the
// top of the stack is greater
// than the current element
if (S.get(i) < s.peek())
{
s.add(S.get(i));
nix = Math.min(nix, S.get(i));
}
else
{
int val = S.get(i);
// Loop to pop the element out
while (!s.isEmpty() && val >= s.peek())
{
s.pop();
}
nix = Math.min(nix, S.get(i));
s.add(nix);
}
}
// The size of the stack is the
// max no of subarrays for
// suffix till index i
// from the right
ans += s.size();
}
return ans;
}
// Driver Code
public static void main(String args[])
{
Vector S = new Vector();
S.add(9);
S.add(6);
S.add(9);
S.add(35);
System.out.println(decompose(S));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation to find the
# sum of Decomposition values of
# all suffixes of an array
import sys
# Function to find the decomposition
# values of the array
def decompose(S):
# Stack
s = []
N = len(S)
ans = 0
# Variable to maintain
# min value in stack
nix = sys.maxsize
# Loop to iterate over the array
for i in range(N - 1, -1, -1):
# Condition to check if the
# stack is empty
if (len(s) == 0):
s.append(S[i])
nix = S[i]
else:
# Condition to check if the
# top of the stack is greater
# than the current element
if (S[i] < s[-1]):
s.append(S[i])
nix = min(nix, S[i])
else:
val = S[i]
# Loop to pop the element out
while (len(s) != 0 and
val >= s[-1]):
s.pop()
nix = min(nix, S[i]);
s.append(nix)
# The size of the stack is the
# max no of subarrays for
# suffix till index i
# from the right
ans += len(s)
return ans
# Driver Code
if __name__ =="__main__":
S = [ 9, 6, 9, 35 ]
print(decompose(S))
# This code is contributed by chitranayal
C#
// C# implementation to find the
// sum of Decomposition values of
// all suffixes of an array
using System;
using System.Collections.Generic;
class GFG{
// Function to find the decomposition
// values of the array
static int decompose(List S)
{
// Stack
Stack s = new Stack();
int N = S.Count;
int ans = 0;
// Variable to maintain
// min value in stack
int nix = Int32.MaxValue;
// Loop to iterate over the array
for(int i = N - 1; i >= 0; i--)
{
// Condition to check if the
// stack is empty
if (s.Count == 0)
{
s.Push(S[i]);
nix = S[i];
}
else
{
// Condition to check if the
// top of the stack is greater
// than the current element
if (S[i] < s.Peek())
{
s.Push(S[i]);
nix = Math.Min(nix, S[i]);
}
else
{
int val = S[i];
// Loop to pop the element out
while (s.Count != 0 && val >= s.Peek())
{
s.Pop();
}
nix = Math.Min(nix, S[i]);
s.Push(nix);
}
}
// The size of the stack is the
// max no of subarrays for
// suffix till index i
// from the right
ans += s.Count;
}
return ans;
}
// Driver code
static void Main()
{
List S = new List();
S.Add(9);
S.Add(6);
S.Add(9);
S.Add(35);
Console.WriteLine(decompose(S));
}
}
// This code is contributed by divyeshrabadiya07
Javascript
8
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live