通过重复删除小于下一个元素的元素来最小化数组的长度
给定一个由N个整数组成的数组arr[] ,任务是重复删除小于下一个元素的元素。
例子:
Input: arr[] = {20, 10, 25, 30, 40}
Output: 40
Explanation:
Below are the operations that are performed:
- Current array: arr[] = {20, 10, 25, 30, 40}
Currently, arr[1](= 10) is less than arr[2](= 25). Therefore, removing arr[1] modifies the array to {20, 25, 30, 40}. - Currently arr[0](= 20) is less than arr[1](= 25). Therefore, removing arr[0] modifies the array to {25, 30, 40}.
- Currently, arr[0](= 25) is less than arr[1](= 30). Therefore, removing arr[0] modifies the array to {30, 40}.
- Currently, arr[0](= 30) is less than arr[1](= 40). Therefore, removing arr[0] modifies the array to {40}.
Therefore, the remaining array is arr[] = {40}.
Input: arr[] = {2, 5, 1, 0}
Output: 5 1 0
朴素方法:解决问题的最简单方法是遍历数组,如果[i + 1, N – 1]范围内有任何更大的元素,则删除该元素。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法——迭代:上述方法可以通过从数组末尾遍历并跟踪找到的最大元素并在当前元素小于最大元素时删除当前元素来优化。 请按照以下步骤解决问题:
- 初始化一个向量,比如res ,以存储结果数组。
- 初始化一个变量,比如maxi为INT_MIN ,以存储最后的最大值。
- 以相反的方式遍历数组arr[]并执行以下步骤:
- 如果arr[i]的值小于最大值,则继续。
- 否则,将元素arr[i]推入res并将maxi的值更新为maxi和arr[i]的最大值。
- 反转向量res 。
- 完成上述步骤后,打印矢量res作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to minimize length of an
// array by repeatedly removing elements
// that are smaller than the next element
void DeleteSmaller(int arr[], int N)
{
// Stores the maximum value in
// the range [i, N]
int maxi = INT_MIN;
// Stores the resultant array
vector res;
// Iterate until i is atleast 0
for (int i = N - 1; i >= 0; i--) {
// If arr[i] is less than maxi
if (arr[i] < maxi)
continue;
// Push the arr[i] in res
res.push_back(arr[i]);
// Update the value of maxi
maxi = max(maxi, arr[i]);
}
// Reverse the vector res
reverse(res.begin(), res.end());
// Print the vector res
for (auto x : res)
cout << x << " ";
}
// Driver Code
int main()
{
int arr[] = { 2, 5, 1, 0 };
int N = sizeof(arr) / sizeof(arr[0]);
DeleteSmaller(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to minimize length of an
// array by repeatedly removing elements
// that are smaller than the next element
static void DeleteSmaller(int arr[], int N)
{
// Stores the maximum value in
// the range [i, N]
int maxi = Integer.MIN_VALUE;
// Stores the resultant array
List res = new ArrayList();
// Iterate until i is atleast 0
for (int i = N - 1; i >= 0; i--) {
// If arr[i] is less than maxi
if (arr[i] < maxi)
continue;
// Push the arr[i] in res
res.add(arr[i]);
// Update the value of maxi
maxi = Math.max(maxi, arr[i]);
}
// Reverse the vector res
Collections.reverse(res);
// Print the vector res
for(int i = 0; i < res.size(); i++)
System.out.println(res.get(i) + " ");
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 5, 1, 0 };
int N = arr.length;
DeleteSmaller(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal
Python3
# Python3 program for the above approach
# Function to minimize length of an
# array by repeatedly removing elements
# that are smaller than the next element
def DeleteSmaller(arr, N):
# Stores the maximum value in
# the range [i, N]
maxi =-10**9
# Stores the resultant array
res = []
# Iterate until i is atleast 0
for i in range(N - 1, -1, -1):
# If arr[i] is less than maxi
if (arr[i] < maxi):
continue
# Push the arr[i] in res
res.append(arr[i])
# Update the value of maxi
maxi = max(maxi, arr[i])
# Reverse the vector res
res = res[::-1]
# Print vector res
print(*res)
# Driver Code
if __name__ == '__main__':
arr = [2, 5, 1, 0]
N = len(arr)
DeleteSmaller(arr, N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to minimize length of an
// array by repeatedly removing elements
// that are smaller than the next element
static void DeleteSmaller(int[] arr, int N)
{
// Stores the maximum value in
// the range [i, N]
int maxi = Int32.MinValue;
// Stores the resultant array
List res = new List();
// Iterate until i is atleast 0
for (int i = N - 1; i >= 0; i--) {
// If arr[i] is less than maxi
if (arr[i] < maxi)
continue;
// Push the arr[i] in res
res.Add(arr[i]);
// Update the value of maxi
maxi = Math.Max(maxi, arr[i]);
}
// Reverse the vector res
res.Reverse();
// Print the vector res
foreach(int x in res)
Console.Write(x + " ");
}
// Driver Code
public static void Main()
{
int[] arr = { 2, 5, 1, 0 };
int N = arr.Length;
DeleteSmaller(arr, N);
}
}
// This code is contributed by SURENDRA_GANGWAR
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Recursive function to remove
// all elements which are smaller
// than the next element
int RecursiveDeleteFunction(
int arr[], int N, int i,
vector& res)
{
// If i is equal to N
if (i == N)
return INT_MIN;
// Recursive call to the next
// element
int curr = RecursiveDeleteFunction(
arr, N, i + 1, res);
// If arr[i] is greater than the
// or equal to curr
if (arr[i] >= curr) {
// Push the arr[i] in res
res.push_back(arr[i]);
// Update the value curr
curr = max(arr[i], curr);
}
// Return the value of curr
return curr;
}
// Function to minimize length of an
// array by repeatedly removing elements
// that are smaller than the next element
void DeleteSmaller(int arr[], int N)
{
// Stores maximum value in the
// the range [i, N]
int maxi = INT_MIN;
// Stores the resultant array
vector res;
// Recursive call to function
// RecursiveDeleteFunction
RecursiveDeleteFunction(arr, N, 0, res);
// Reverse the vector res
reverse(res.begin(), res.end());
// Print the vector res
for (auto x : res)
cout << x << " ";
}
// Driver Code
int main()
{
int arr[] = { 2, 5, 1, 0 };
int N = sizeof(arr) / sizeof(arr[0]);
DeleteSmaller(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Recursive function to remove
// all elements which are smaller
// than the next element
static int RecursiveDeleteFunction(
int []arr, int N, int i, ArrayList res)
{
// If i is equal to N
if (i == N)
return Integer.MIN_VALUE;
// Recursive call to the next
// element
int curr = RecursiveDeleteFunction(
arr, N, i + 1, res);
// If arr[i] is greater than the
// or equal to curr
if (arr[i] >= curr) {
// Push the arr[i] in res
res.add(arr[i]);
// Update the value curr
curr = Math.max(arr[i], curr);
}
// Return the value of curr
return curr;
}
// Function to minimize length of an
// array by repeatedly removing elements
// that are smaller than the next element
static void DeleteSmaller(int []arr, int N)
{
// Stores maximum value in the
// the range [i, N]
int maxi = Integer.MIN_VALUE;
// Stores the resultant array
ArrayList
res = new ArrayList();
// Recursive call to function
// RecursiveDeleteFunction
RecursiveDeleteFunction(arr, N, 0, res);
// Reverse the vector res
Collections.reverse(res);
// Print the vector res
for (int i = 0; i < res.size(); i++)
System.out.print(res.get(i) + " ");
}
// Driver Code
public static void main(String args[])
{
int []arr = { 2, 5, 1, 0 };
int N = arr.length;
DeleteSmaller(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python3 program for the above approach
# Recursive function to remove
# all elements which are smaller
# than the next element
def RecursiveDeleteFunction(arr, N, i, res) :
# If i is equal to N
if (i == N) :
return -10**9
# Recursive call to the next
# element
curr = RecursiveDeleteFunction(
arr, N, i + 1, res)
# If arr[i] is greater than the
# or equal to curr
if (arr[i] >= curr) :
# Push the arr[i] in res
res.append(arr[i])
# Update the value curr
curr = max(arr[i], curr)
# Return the value of curr
return curr
# Function to minimize length of an
# array by repeatedly removing elements
# that are smaller than the next element
def DeleteSmaller(arr, N) :
# Stores maximum value in the
# the range [i, N]
maxi = -10**9
# Stores the resultant array
res = []
# Recursive call to function
# RecursiveDeleteFunction
RecursiveDeleteFunction(arr, N, 0, res)
# Reverse the vector res
res = res[::-1]
# Print the vector res
for x in res :
print(x, end = " ")
# Driver Code
if __name__ == '__main__':
arr = [2, 5, 1, 0]
N = len(arr)
DeleteSmaller(arr, N)
# This code is contributed by sanjoy_62.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Recursive function to remove
// all elements which are smaller
// than the next element
static int RecursiveDeleteFunction(
int []arr, int N, int i, List res)
{
// If i is equal to N
if (i == N)
return Int32.MinValue;
// Recursive call to the next
// element
int curr = RecursiveDeleteFunction(
arr, N, i + 1, res);
// If arr[i] is greater than the
// or equal to curr
if (arr[i] >= curr) {
// Push the arr[i] in res
res.Add(arr[i]);
// Update the value curr
curr = Math.Max(arr[i], curr);
}
// Return the value of curr
return curr;
}
// Function to minimize length of an
// array by repeatedly removing elements
// that are smaller than the next element
static void DeleteSmaller(int []arr, int N)
{
// Stores maximum value in the
// the range [i, N]
int maxi = Int32.MinValue;
// Stores the resultant array
List res = new List();
// Recursive call to function
// RecursiveDeleteFunction
RecursiveDeleteFunction(arr, N, 0, res);
// Reverse the vector res
res.Reverse();
// Print the vector res
for (int i = 0; i < res.Count; i++)
Console.Write(res[i] + " ");
}
// Driver Code
public static void Main()
{
int []arr = { 2, 5, 1, 0 };
int N = arr.Length;
DeleteSmaller(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
5 1 0
时间复杂度: O(N)
辅助空间: O(N)
高效方法——递归:上述方法也可以使用递归来实现。请按照以下步骤解决问题:
- 初始化一个向量,比如res来存储结果数组。
- 定义一个递归函数,比如RecursiveDeleteFunction(int i)来删除小于下一个元素的元素:
- 如果i的值等于N则返回INT_MIN 。
- 递归调用函数RecursiveDeleteFunction(i+1)并将返回的值存储在变量中,比如curr 。
- 如果arr[i]的值至少为 curr ,则将arr[i]推入向量res并将curr的值更新为curr和arr[i]的最大值。
- 现在,返回curr 。
- 调用函数RecursiveDeleteFunction(0)将较小的元素删除到下一个元素,然后反转向量res 。
- 完成上述步骤后,打印矢量res作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Recursive function to remove
// all elements which are smaller
// than the next element
int RecursiveDeleteFunction(
int arr[], int N, int i,
vector& res)
{
// If i is equal to N
if (i == N)
return INT_MIN;
// Recursive call to the next
// element
int curr = RecursiveDeleteFunction(
arr, N, i + 1, res);
// If arr[i] is greater than the
// or equal to curr
if (arr[i] >= curr) {
// Push the arr[i] in res
res.push_back(arr[i]);
// Update the value curr
curr = max(arr[i], curr);
}
// Return the value of curr
return curr;
}
// Function to minimize length of an
// array by repeatedly removing elements
// that are smaller than the next element
void DeleteSmaller(int arr[], int N)
{
// Stores maximum value in the
// the range [i, N]
int maxi = INT_MIN;
// Stores the resultant array
vector res;
// Recursive call to function
// RecursiveDeleteFunction
RecursiveDeleteFunction(arr, N, 0, res);
// Reverse the vector res
reverse(res.begin(), res.end());
// Print the vector res
for (auto x : res)
cout << x << " ";
}
// Driver Code
int main()
{
int arr[] = { 2, 5, 1, 0 };
int N = sizeof(arr) / sizeof(arr[0]);
DeleteSmaller(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Recursive function to remove
// all elements which are smaller
// than the next element
static int RecursiveDeleteFunction(
int []arr, int N, int i, ArrayList res)
{
// If i is equal to N
if (i == N)
return Integer.MIN_VALUE;
// Recursive call to the next
// element
int curr = RecursiveDeleteFunction(
arr, N, i + 1, res);
// If arr[i] is greater than the
// or equal to curr
if (arr[i] >= curr) {
// Push the arr[i] in res
res.add(arr[i]);
// Update the value curr
curr = Math.max(arr[i], curr);
}
// Return the value of curr
return curr;
}
// Function to minimize length of an
// array by repeatedly removing elements
// that are smaller than the next element
static void DeleteSmaller(int []arr, int N)
{
// Stores maximum value in the
// the range [i, N]
int maxi = Integer.MIN_VALUE;
// Stores the resultant array
ArrayList
res = new ArrayList();
// Recursive call to function
// RecursiveDeleteFunction
RecursiveDeleteFunction(arr, N, 0, res);
// Reverse the vector res
Collections.reverse(res);
// Print the vector res
for (int i = 0; i < res.size(); i++)
System.out.print(res.get(i) + " ");
}
// Driver Code
public static void main(String args[])
{
int []arr = { 2, 5, 1, 0 };
int N = arr.length;
DeleteSmaller(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python3 program for the above approach
# Recursive function to remove
# all elements which are smaller
# than the next element
def RecursiveDeleteFunction(arr, N, i, res) :
# If i is equal to N
if (i == N) :
return -10**9
# Recursive call to the next
# element
curr = RecursiveDeleteFunction(
arr, N, i + 1, res)
# If arr[i] is greater than the
# or equal to curr
if (arr[i] >= curr) :
# Push the arr[i] in res
res.append(arr[i])
# Update the value curr
curr = max(arr[i], curr)
# Return the value of curr
return curr
# Function to minimize length of an
# array by repeatedly removing elements
# that are smaller than the next element
def DeleteSmaller(arr, N) :
# Stores maximum value in the
# the range [i, N]
maxi = -10**9
# Stores the resultant array
res = []
# Recursive call to function
# RecursiveDeleteFunction
RecursiveDeleteFunction(arr, N, 0, res)
# Reverse the vector res
res = res[::-1]
# Print the vector res
for x in res :
print(x, end = " ")
# Driver Code
if __name__ == '__main__':
arr = [2, 5, 1, 0]
N = len(arr)
DeleteSmaller(arr, N)
# This code is contributed by sanjoy_62.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Recursive function to remove
// all elements which are smaller
// than the next element
static int RecursiveDeleteFunction(
int []arr, int N, int i, List res)
{
// If i is equal to N
if (i == N)
return Int32.MinValue;
// Recursive call to the next
// element
int curr = RecursiveDeleteFunction(
arr, N, i + 1, res);
// If arr[i] is greater than the
// or equal to curr
if (arr[i] >= curr) {
// Push the arr[i] in res
res.Add(arr[i]);
// Update the value curr
curr = Math.Max(arr[i], curr);
}
// Return the value of curr
return curr;
}
// Function to minimize length of an
// array by repeatedly removing elements
// that are smaller than the next element
static void DeleteSmaller(int []arr, int N)
{
// Stores maximum value in the
// the range [i, N]
int maxi = Int32.MinValue;
// Stores the resultant array
List res = new List();
// Recursive call to function
// RecursiveDeleteFunction
RecursiveDeleteFunction(arr, N, 0, res);
// Reverse the vector res
res.Reverse();
// Print the vector res
for (int i = 0; i < res.Count; i++)
Console.Write(res[i] + " ");
}
// Driver Code
public static void Main()
{
int []arr = { 2, 5, 1, 0 };
int N = arr.Length;
DeleteSmaller(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出:
5 1 0
时间复杂度: O(N)
辅助空间: O(N)