📌  相关文章
📜  删除元素,使每个有效i的数组满足arr [i + 1] <arr [i]

📅  最后修改于: 2021-05-31 22:31:22             🧑  作者: Mango

给定非负整数的数组arr [] 。我们必须从此数组中删除元素,以使每个有效i的arr [i + 1]> arr [j]都将被计为一个步骤。我们必须应用相同的操作,直到数组严格减小为止。现在的任务是计算获得所需阵列所需的步骤数。

例子:

方法:想法是仅保留要针对特定元素进行检查的必需元素的索引。因此,我们使用向量仅存储所需的索引。我们将每个索引插入后面,如果满足以下条件,则从后面删除索引。

我们采用另一个数组,在该数组中我们更新特定元素要删除的步骤。
如果status [i] = -1,则不删除元素,0表示第一步,依此类推。这就是为什么我们将答案加1。
弹出索引时,我们反复更新元素的状态。如果弹出所有索引,即vect.size()= 0,则不删除此元素,因此将其状态更改为-1

下面是上述方法的实现:

CPP
// C++ implementation of the approach
#include 
using namespace std;
int status[100000];
  
// Function to return the required
// number of steps
int countSteps(int* val, int n)
{
    int sol = 0;
    vector vec(1, 0);
    status[0] = -1;
  
    // Compute the number of steps
    for (int i = 1; i < n; ++i) {
  
        // Current status is to
        // delete in first step
        status[i] = 0;
  
        // Pop the indices while
        // condition is satisfied
        while (vec.size() > 0
               && val[vec.back()] >= val[i]) {
  
            // Inserting the correct
            // step no to delete
            status[i] = max(status[i],
                            status[vec.back()] + 1);
            vec.pop_back();
        }
        if (vec.size() == 0) {
  
            // Status changed to not delete
            status[i] = -1;
        }
  
        // Pushing a new index in the vector
        vec.push_back(i);
  
        // Build the solution from
        // smaller to larger size
        sol = max(sol, status[i] + 1);
    }
    return sol;
}
  
// Driver code
int main()
{
    int val[] = { 6, 5, 8, 4, 7, 10, 9 };
    int n = sizeof(val) / sizeof(val[0]);
  
    cout << countSteps(val, n);
  
    return 0;
}


Java
// A Java implementation of the approach
import java.util.*;
  
class GFG 
{
      
static int []status = new int[100000];
  
// Function to return the required
// number of steps
static int countSteps(int[]val, int n)
{
    int sol = 0;
    Vector vec = new Vector<>(1);
    vec.add(0);
    status[0] = -1;
  
    // Compute the number of steps
    for (int i = 1; i < n; ++i) 
    {
  
        // Current status is to
        // delete in first step
        status[i] = 0;
  
        // Pop the indices while
        // condition is satisfied
        while (vec.size() > 0
            && val[vec.lastElement()] >= val[i])
        {
  
            // Inserting the correct
            // step no to delete
            status[i] = Math.max(status[i],
                            status[vec.lastElement()] + 1);
            vec.remove(vec.lastElement());
        }
        if (vec.isEmpty())
        {
  
            // Status changed to not delete
            status[i] = -1;
        }
  
        // Pushing a new index in the vector
        vec.add(i);
  
        // Build the solution from
        // smaller to larger size
        sol = Math.max(sol, status[i] + 1);
    }
    return sol;
}
  
// Driver code
public static void main(String[] args) 
{
    int val[] = { 6, 5, 8, 4, 7, 10, 9 };
    int n = val.length;
  
    System.out.println(countSteps(val, n));
}
}
  
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 implementation of the approach 
  
status = [0]*100000; 
  
# Function to return the required 
# number of steps 
def countSteps(val, n) :
      
    sol = 0; 
    vec = [1, 0]; 
    status[0] = -1; 
  
    # Compute the number of steps 
    for i in range(n) :
  
        # Current status is to 
        # delete in first step 
        status[i] = 0; 
  
        # Pop the indices while 
        # condition is satisfied 
        while (len(vec) > 0
            and val[vec[len(vec)-1]] >= val[i]) : 
  
            # Inserting the correct 
            # step no to delete 
            status[i] = max(status[i], 
                            status[len(vec)-1] + 1); 
            vec.pop(); 
          
        if (len(vec) == 0) :
  
            # Status changed to not delete 
            status[i] = -1; 
          
  
        # Pushing a new index in the vector 
        vec.append(i); 
  
        # Build the solution from 
        # smaller to larger size 
        sol = max(sol, status[i] + 1); 
      
    return sol; 
  
  
# Driver code 
if __name__ == "__main__" : 
  
    val = [ 6, 5, 8, 4, 7, 10, 9 ]; 
    n = len(val); 
  
    print(countSteps(val, n)); 
      
# This code is contributed by AnkitRai01


C#
// A C# implementation of the approach 
using System;
using System.Collections.Generic;
  
class GFG 
{ 
      
static int []status = new int[100000]; 
  
// Function to return the required 
// number of steps 
static int countSteps(int[]val, int n) 
{ 
    int sol = 0; 
    List vec = new List(1); 
    vec.Add(0); 
    status[0] = -1; 
  
    // Compute the number of steps 
    for (int i = 1; i < n; ++i) 
    { 
  
        // Current status is to 
        // delete in first step 
        status[i] = 0; 
  
        // Pop the indices while 
        // condition is satisfied 
        while (vec.Count > 0
            && val[vec[vec.Count-1]] >= val[i]) 
        { 
  
            // Inserting the correct 
            // step no to delete 
            status[i] = Math.Max(status[i], 
                            status[vec[vec.Count-1]] + 1); 
            vec.Remove(vec[vec.Count-1]); 
        } 
        if (vec.Count == 0) 
        { 
  
            // Status changed to not delete 
            status[i] = -1; 
        } 
  
        // Pushing a new index in the vector 
        vec.Add(i); 
  
        // Build the solution from 
        // smaller to larger size 
        sol = Math.Max(sol, status[i] + 1); 
    } 
    return sol; 
} 
  
// Driver code 
public static void Main(String[] args) 
{ 
    int []val = { 6, 5, 8, 4, 7, 10, 9 }; 
    int n = val.Length; 
  
    Console.WriteLine(countSteps(val, n)); 
} 
} 
  
// This code contributed by Rajput-Ji


输出:
2

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。