给定一个由N个整数组成的数组arr [] ,任务是从给定数组的左边删除小于该元素的元素。继续从数组中删除元素,直到没有元素具有较小的相邻左元素。完成上述操作后,打印结果数组。
例子:
Input: arr[] = {2, 4, 1, 3, 4}
Output: 2 1
Explanation:
Since 4 is greater than 2 remove 4, and arr become {2, 1, 3, 4}.
Now 3 is greater than 1 so remove 3 and arr become {2, 1, 4}.
Now 4 is greater than 1 so remove 4 and arr become {2, 1}.
Now no elements satisfy the removing criteria so the resultant array is {2, 1}.
Input: arr[] = {5, 4, 3, 2, 1}
Output: 5 4 3 2 1
方法:想法是使用合并排序的概念。
- 将输入数组划分为子数组,直到每个子数组的大小变为1。
- 开始合并元素。
- 合并时,从左侧子数组中删除元素,直到其最右边的元素,其值大于右侧子数组的最左边的元素。
- 在每个合并步骤中重复上述步骤,以便删除所有值小于其剩余值的元素。
- 最后打印结果数组。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to implement merging of arr[]
vector merge(vector x, vector y)
{
for(auto i : y)
{
if (x[x.size() - 1] > i)
x.push_back(i);
}
return x;
}
// Function to delete all elements which
// satisfy the condition A[i] > A[i-1]
vector mergeDel(vector l)
{
// Divide array into its subarray
if (l.size() == 1)
return l;
int m = l.size() / 2;
vector temp1 = {l.begin() + 0,
l.begin() + m};
vector temp2 = {l.begin() + m,
l.end()};
// Getting back merged array with all
// its right element greater than
// left one.
return merge(mergeDel(temp1),
mergeDel(temp2));
}
// Driver Code
int main()
{
// Given array arr[]
vector arr({ 5, 4, 3, 2, 1 });
vector ans = mergeDel(arr);
cout << "[ ";
for(auto x: ans)
cout << x << ", ";
cout << "]";
}
// This code is contributed by SURENDRA_GANGWAR
Java
// Java program for the above approach
import java.util.ArrayList;
import java.util.Arrays;
class GFG{
// Function to implement merging of arr[]
static ArrayList merge(ArrayList x,
ArrayList y)
{
for(Integer i : y)
{
if (x.get(x.size() - 1) > i)
x.add(i);
}
return x;
}
// Function to delete all elements which
// satisfy the condition A[i] > A[i-1]
static ArrayList mergeDel(ArrayList l)
{
// Divide array into its subarray
if (l.size() == 1)
return l;
int m = l.size() / 2;
ArrayList temp1 = new ArrayList<>(
l.subList(0, m));
ArrayList temp2 = new ArrayList<>(
l.subList(m, l.size()));
// Getting back merged array with all
// its right element greater than
// left one.
return merge(mergeDel(temp1), mergeDel(temp2));
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
Integer[] ar = { 5, 4, 3, 2, 1 };
ArrayList arr = new ArrayList<>(
Arrays.asList(ar));
ArrayList ans = mergeDel(arr);
System.out.print("[ ");
for(Integer x : ans)
System.out.print(x + ", ");
System.out.println("]");
}
}
// This code is contributed by sanjeev2552
Python3
# Python3 program for the above approach
# Function to delete all elements which
# satisfy the condition A[i] > A[i-1]
def mergeDel(l):
# Divide array into its subarray
if len(l) == 1:
return l
m = int( len(l) / 2)
# Getting back merged array with all
# its right element greater than left one.
return merge(mergeDel(l[ 0 : m ]),
mergeDel(l[ m : len(l)]) )
# Function to implement merging of arr[]
def merge(x, y):
for i in y:
if x[-1] > i :
x = x + [i]
return x
# Driver Code
# Function defination for main()
def main():
# Given array arr[]
arr = [5, 4, 3, 2, 1]
print(mergeDel(arr))
main()
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to implement merging of arr[]
static List merge(List x, List y)
{
foreach(int i in y)
{
if (x[x.Count - 1] > i)
x.Add(i);
}
return x;
}
// Function to delete all elements which
// satisfy the condition A[i] > A[i-1]
static List mergeDel(List l)
{
// Divide array into its subarray
if (l.Count == 1)
return l;
int m = l.Count / 2;
List temp1 = l.GetRange(0, m);
List temp2 = l.GetRange(m, l.Count - m);
// Getting back merged array with all
// its right element greater than
// left one.
return merge(mergeDel(temp1),
mergeDel(temp2));
}
// Driver Code
public static void Main(string[] args)
{
// Given array arr[]
List arr = new List{ 5, 4, 3, 2, 1 };
List ans = mergeDel(arr);
Console.Write("[ ");
foreach(int x in ans)
Console.Write(x + ", ");
Console.Write("]");
}
}
// This code is contributed by chitranayal
输出:
[5, 4, 3, 2, 1]
时间复杂度: O(N * log N)
辅助空间: O(1)