通过删除最小数量的元素将给定的数组设为 Mountain 数组
给定一个长度为N的数组arr[] ,任务是从数组中删除最小数量的元素,使其成为一个山数组,然后打印它。
Note: A mountain array is an array where there is an index i such that arr[0] < arr[1] < . . .< arr[i-1] < arr[i] > arr[i+1] > . . . > arr[N-1]. Also a mountain array must have a length greater than or equal to 3 to satisfy the above condition.
例子:
Input: arr[] = {9, 8, 1, 7, 6, 5, 4, 3, 2, 1}
Output: 1 7 6 5 4 3 2 1
Explanation: Remove the elements 9, 8. The resultant array is the mountain array.
It is the minimum number of removals possible.
Input: arr[] = {1, 1, 1};
Output: -1
Explanation: This array cannot be transformed into a mountain array
方法:按照以下步骤解决此问题:
- 创建两个数组left和right 。
- 对于每个索引i , left[i]将存储以i结束的最长递增子序列,而right[i]将存储从i 开始的最长递减子序列。
- 现在,假设每个索引都是山峰,找到山子序列的最大长度。
Length of mountain subsequence having peak at i = left[i]+right[i]-1
- 找到山子序列的最大长度,比如max并跟踪达到最大长度的峰值。
- 因此,最小删除数为(N – max) 。
- 现在打印从开始到i的最长递增子序列和从i到数组末尾的最长递减子序列。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print the mountain array
// after minimum removals
void mountArray(vector& arr)
{
int N = arr.size();
vector left(N, 1), right(N, 1);
for (int i = 0; i < N; ++i) {
for (int j = 0; j <= i; ++j) {
if (arr[j] < arr[i]) {
// This means the
// previous number is
// smaller than the number
left[i] = max(left[j] + 1, left[i]);
}
}
}
// Find the longest decreasing sequence
for (int i = N - 1; i >= 0; --i) {
for (int j = i; j <= N - 1; ++j) {
if (arr[j] < arr[i]) {
right[i] = max(right[j] + 1, right[i]);
}
}
}
int max = 0;
int index = 0;
for (int i = 1; i < N - 1; ++i) {
if (left[i] != 1 && right[i] != 1) {
if (max < (left[i] + right[i]) - 1) {
index = i;
max = (left[i] + right[i]) - 1;
}
}
}
// Print the longest continuous
// subsequence from 0 to ith
// and ith to N index
vector left1;
left1.push_back(arr[index]);
for (int i = index; i >= 0; --i) {
if (arr[i] < arr[index]) {
// There is possibility
// either the index is
// used or not
if (left[i] + 1 == left[index]) {
left1.push_back(arr[i]);
left[index] -= 1;
}
}
}
vector right1;
// Starting the right
for (int i = index; i < right.size(); ++i) {
if (arr[index] > arr[i]) {
if (right[i] + 1 == right[index]) {
right1.push_back(arr[i]);
right[index] -= 1;
}
}
}
if (max < 3) {
cout << (-1) << "\n";
}
else {
// Print the first left1 array
// then the right array
for (int i = left1.size() - 1; i >= 0; --i) {
cout << left1[i] << " ";
}
for (int i = 0; i < right1.size(); ++i) {
cout << right1[i] << " ";
}
}
}
// Driver code
int main()
{
vector arr = { 9, 8, 1, 7, 6, 5, 4, 3, 2, 1 };
mountArray(arr);
}
// This code is contributed by Taranpreet
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to print the mountain array
// after minimum removals
public static void mountArray(int arr[])
{
int N = arr.length;
int left[] = new int[N];
int right[] = new int[N];
Arrays.fill(left, 1);
Arrays.fill(right, 1);
for (int i = 0; i < N; ++i) {
for (int j = 0; j <= i; ++j) {
if (arr[j] < arr[i]) {
// This means the
// previous number is
// smaller than the number
left[i] = Math.max(left[j]
+ 1,
left[i]);
}
}
}
// Find the longest decreasing sequence
for (int i = N - 1; i >= 0; --i) {
for (int j = i; j <= N - 1; ++j) {
if (arr[j] < arr[i]) {
right[i]
= Math.max(right[j]
+ 1,
right[i]);
}
}
}
int max = 0;
int index = 0;
for (int i = 1; i < N - 1; ++i) {
if (left[i] != 1
&& right[i] != 1) {
if (max < (left[i]
+ right[i])
- 1) {
index = i;
max = (left[i]
+ right[i])
- 1;
}
}
}
// Print the longest continuous
// subsequence from 0 to ith
// and ith to N index
ArrayList left1
= new ArrayList();
left1.add(arr[index]);
for (int i = index; i >= 0; --i) {
if (arr[i] < arr[index]) {
// There is possibility
// either the index is
// used or not
if (left[i] + 1 == left[index]) {
left1.add(arr[i]);
left[index] -= 1;
}
}
}
ArrayList right1
= new ArrayList<>();
// Starting the right
for (int i = index; i
< right.length;
++i) {
if (arr[index] > arr[i]) {
if (right[i] + 1
== right[index]) {
right1.add(arr[i]);
right[index] -= 1;
}
}
}
if (max < 3) {
System.out.println(-1);
}
else {
// Print the first left1 array
// then the right array
for (int i = left1.size() - 1;
i >= 0; --i) {
System.out.print(left1.get(i)
+ " ");
}
for (int i = 0; i < right1.size();
++i) {
System.out.print(right1.get(i)
+ " ");
}
}
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 9, 8, 1, 7, 6, 5, 4, 3, 2, 1 };
mountArray(arr);
}
}
Python3
# python3 program for the above approach
# Function to print the mountain array
# after minimum removals
def mountArray(arr):
N = len(arr)
left, right = [1 for _ in range(N)], [1 for _ in range(N)]
for i in range(0, N):
for j in range(0, i+1):
if (arr[j] < arr[i]):
# This means the
# previous number is
# smaller than the number
left[i] = max(left[j] + 1, left[i])
# Find the longest decreasing sequence
for i in range(N-1, -1, -1):
for j in range(i, N):
if (arr[j] < arr[i]):
right[i] = max(right[j] + 1, right[i])
maxi = 0
index = 0
for i in range(1, N-1):
if (left[i] != 1 and right[i] != 1):
if (maxi < (left[i] + right[i]) - 1):
index = i
maxi = (left[i] + right[i]) - 1
# Print the longest continuous
# subsequence from 0 to ith
# and ith to N index
left1 = []
left1.append(arr[index])
for i in range(index, -1, -1):
if (arr[i] < arr[index]):
# There is possibility
# either the index is
# used or not
if (left[i] + 1 == left[index]):
left1.append(arr[i])
left[index] -= 1
right1 = []
# Starting the right
for i in range(index, len(right)):
if (arr[index] > arr[i]):
if (right[i] + 1 == right[index]):
right1.append(arr[i])
right[index] -= 1
if (maxi < 3):
print("-1")
else:
# Print the first left1 array
# then the right array
for i in range(len(left1) - 1, -1, -1):
print(left1[i], end=" ")
for i in range(0, len(right1)):
print(right1[i], end=" ")
# Driver code
if __name__ == "__main__":
arr = [9, 8, 1, 7, 6, 5, 4, 3, 2, 1]
mountArray(arr)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections;
class GFG {
// Function to print the mountain array
// after minimum removals
static void mountArray(int []arr)
{
int N = arr.Length;
int []left = new int[N];
int []right = new int[N];
for(int i = 0; i < N; i++) {
left[i] = 1;
right[i] = 1;
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j <= i; ++j) {
if (arr[j] < arr[i]) {
// This means the
// previous number is
// smaller than the number
left[i] = Math.Max(left[j]
+ 1,
left[i]);
}
}
}
// Find the longest decreasing sequence
for (int i = N - 1; i >= 0; --i) {
for (int j = i; j <= N - 1; ++j) {
if (arr[j] < arr[i]) {
right[i]
= Math.Max(right[j]
+ 1,
right[i]);
}
}
}
int max = 0;
int index = 0;
for (int i = 1; i < N - 1; ++i) {
if (left[i] != 1
&& right[i] != 1) {
if (max < (left[i]
+ right[i])
- 1) {
index = i;
max = (left[i]
+ right[i])
- 1;
}
}
}
// Print the longest continuous
// subsequence from 0 to ith
// and ith to N index
ArrayList left1 = new ArrayList();
left1.Add(arr[index]);
for (int i = index; i >= 0; --i) {
if (arr[i] < arr[index]) {
// There is possibility
// either the index is
// used or not
if (left[i] + 1 == left[index]) {
left1.Add(arr[i]);
left[index] -= 1;
}
}
}
ArrayList right1 = new ArrayList();
// Starting the right
for (int i = index; i
< right.Length;
++i) {
if (arr[index] > arr[i]) {
if (right[i] + 1
== right[index]) {
right1.Add(arr[i]);
right[index] -= 1;
}
}
}
if (max < 3) {
Console.Write(-1);
}
else {
// Print the first left1 array
// then the right array
for (int i = left1.Count - 1;
i >= 0; --i) {
Console.Write(left1[i]
+ " ");
}
for (int i = 0; i < right1.Count;
++i) {
Console.Write(right1[i]
+ " ");
}
}
}
// Driver code
public static void Main()
{
int []arr = { 9, 8, 1, 7, 6, 5, 4, 3, 2, 1 };
mountArray(arr);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
1 7 6 5 4 3 2 1
时间复杂度: O(N 2 )
辅助空间: O(N)