📌  相关文章
📜  通过删除最大值及其右侧使数组为空的步骤

📅  最后修改于: 2022-05-13 01:57:50.365000             🧑  作者: Mango

通过删除最大值及其右侧使数组为空的步骤

我们得到一个整数数组。我们必须对数组执行以下操作,直到它完全耗尽。

  • 选择数组中的最大数字并删除该数字,包括数组右侧的所有数字。
  • 对数组的左侧元素重复步骤 1,即选择左侧元素中的最大元素并将其删除,包括其右侧的所有数字。

我们的任务是模拟上述过程并返回在数组的第一个元素(索引 0)也被删除并且数组被耗尽之前将采取的步骤数。
例子:

Input : Array = [2, 3, 5, 4, 1]
Output : Steps Taken: 3
Explanation: Step 1: Remove 5 and elements to its right
             so, Array becomes [2, 3]
             Step 2: Remove 3 as it is the maximum and 
             right most already so, Array becomes [2]
             Step 3: Remove 2 and the array becomes EMPTY
             Hence, at the end of step 3 the array stands 
             exhausted.

Input : Array = [2, 5, 8, 24, 4, 11, 6, 1, 15, 10]
Output : Steps Taken: 4
Explanation: Step 1: Remove 24 and elements to its right
             so, Array becomes [2, 5, 8]
             Step 2: Remove 8 and elements to its right
             so, Array becomes [2, 5]
             Step 3: Remove 5 and elements to its right
             so, Array becomes [2]
             Step 4: Remove 2 and the array becomes EMPTY
             Hence, at the end of step 4 the array stands 
             exhausted.

天真的方法:
解决这个问题的一个简单方法是在数组中找到最大值并存储它的索引,然后再次在数组中找到范围 0 和先前存储的索引之间的最大值。重复此过程,直到删除第 0 个索引元素。

C++
// C++ program to simulate max deletion
// and calculate number of steps.
#include 
using namespace std;
 
// Function to find index of the maximum number
// in the array of size n
int findMax(int arr[], int n)
{
    int max = 0, index = 0;
    for (int i = 0; i < n; i++)
 
        // Condition to get the maximum
        if (arr[i] > max) {
            max = arr[i];
            index = i;
        }
 
    // return the index of the maximum element
    return index;
}
 
int countSteps(int arr[], int n)
{
    // Find the index of largest number in the array
    int index = findMax(arr, n);
 
    //'steps' variable calculates the number of
    // steps being taken.
    int steps = 1;
 
    // Check until the first element of array is removed,
    // hence until index!=0
    while (index != 0) {
 
        // Update index with the index value of highest
        // element in the remaining array.
        index = findMax(arr, index);
        steps++;
    }
 
    return steps;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 5, 8, 24, 4, 11, 6, 1, 15, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << "Steps Taken: ";
    cout << countSteps(arr, n) << endl;
    return 0;
}


Java
// Java program to simulate max deletion
// and calculate number of steps.
import java.util.*;
 
class GFG {
 
    // Function to find index of the maximum
    // number in the array of size n
    static int findMax(int arr[], int n)
    {
        int max = 0, index = 0;
        for (int i = 0; i < n; i++)
 
            // Condition to get the maximum
            if (arr[i] > max) {
                max = arr[i];
                index = i;
            }
 
        // return the index of the maximum
        // element
        return index;
    }
 
    static int countSteps(int arr[], int n)
    {
        // Find the index of largest number
        // in the array
        int index = findMax(arr, n);
 
        //'steps' variable calculates the
        // number of steps being taken.
        int steps = 1;
 
        // Check until the first element
        // of array is removed, hence
        // until index!=0
        while (index != 0) {
 
            // Update index with the index
            // value of highest element in
            // the remaining array.
            index = findMax(arr, index);
            steps++;
        }
 
        return steps;
    }
 
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int arr[] = { 2, 5, 8, 24, 4, 11,
                      6, 1, 15, 10 };
        int n = arr.length;
 
        System.out.print("Steps Taken: ");
        System.out.println(countSteps(arr, n));
    }
}
// This code is contributed by Arnav Kr. Mandal.


Python 3
# Python program to simulate max deletion
# and calculate number of steps.
 
# Function to find index of the maximum number
# in the array of size n
def findMax(arr, n):
    large, index = 0, 0
 
    for i in range(n):
 
        # Condition to get the maximum
        if arr[i] > large:
            large = arr[i]
            index = i
 
    # return the index of the maximum element
    return index
 
 
def countSteps(arr, n):
 
    # Find the index of largest number in the array
    index = findMax(arr, n)
 
    #'steps' variable calculates the number of
    # steps being taken.
    steps = 1
 
    # Check until the first element of array is removed,
    # hence until index != 0
    while index != 0:
 
        # Update index with the index value of highest
        # element in the remaining array.
        index = findMax(arr, index)
        steps += 1
 
    return steps
 
 
# Driver Code
if __name__ == "__main__":
    arr = [2, 5, 8, 24, 4, 11, 6, 1, 15, 10]
    n = len(arr)
    print("Steps Taken:", countSteps(arr, n))
 
# This code is contributed by
# sanjeev2552


C#
// C# program to simulate max
// deletion and calculate
// number of steps.
using System;
 
class GFG {
 
    // Function to find index
    // of the maximum number
    // in the array of size n
    static int findMax(int[] arr, int n)
    {
        int max = 0, index = 0;
        for (int i = 0; i < n; i++)
 
            // Condition to get
            // the maximum
            if (arr[i] > max)
            {
                max = arr[i];
                index = i;
            }
 
        // return the index of the
        // maximum element
        return index;
    }
 
    static int countSteps(int[] arr,
                                int n)
    {
        // Find the index of largest
        // number in the array
        int index = findMax(arr, n);
 
        //'steps' variable calculates
        // the number of steps being
        // taken.
        int steps = 1;
 
        // Check until the first
        // element of array is
        // removed, hence until
        // index!=0
        while (index != 0) {
 
            // Update index with
            // the index value of
            // highest element in
            // the remaining array.
            index = findMax(arr, index);
            steps++;
        }
 
        return steps;
    }
 
    /* Driver program to test
    above function */
    public static void Main()
    {
        int[] arr = { 2, 5, 8, 24, 4,
                  11, 6, 1, 15, 10 };
        int n = arr.Length;
 
        Console.Write("Steps Taken: ");
         
        Console.WriteLine(
                  countSteps(arr, n));
    }
}
 
// This code is contributed by vt_m.


PHP
 $max)
        {
            $max = $arr[$i];
            $index = $i;
        }
 
    // return the index of
    // the maximum element
    return $index;
}
 
function countSteps($arr, $n)
{
    // Find the index of largest
    // number in the array
    $index = findMax($arr, $n);
 
    //'steps' variable calculates
    // the number of steps being taken.
    $steps = 1;
 
    // Check until the first
    // element of array is removed,
    // hence until index!=0
    while ($index != 0)
    {
 
        // Update index with the
        // index value of highest
        // element in the remaining array.
        $index = findMax($arr, $index);
        $steps++;
    }
 
    return $steps;
}
 
    // Driver Code
    $arr = array(2, 5, 8, 24, 4,
              11, 6, 1, 15, 10);
    $n = sizeof($arr);
    echo "Steps Taken: ";
    echo countSteps($arr, $n) ,"\n";
 
// This code is contributed by ajit.
?>


Javascript


CPP
if (max < arr[i]) {
 
    // keep a count of number of times this
    // condition executes.
    max = arr[i];
}


C++
// C++ program to simulate max deletion
// and calculate number of steps.
#include 
using namespace std;
 
// Function to find the maximum number in the
// array upto index n and return its index.
int countSteps(int arr[], int n)
{
    int max = -1, steps = 0;
    for (int i = 0; i < n; i++) {
 
        // condition to find max
        if (arr[i] > max) {
 
            max = arr[i];
 
            // Count the number of times this
            // condition executes that will the
            // number of turns being played.
            steps++;
        }
    }
 
    // return the number of turns played
    return steps;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 5, 8, 24, 4, 11, 6, 1, 15, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    //'steps' variable calculates the number of
    // steps being taken.
    cout << "Steps Taken: ";
    cout << countSteps(arr, n) << endl;
    return 0;
}


Java
// Java code to simulate max deletion
// and calculate number of steps.
import java.util.*;
 
class GFG {
 
    // Function to find the maximum number in the
    // array upto index n and return its index.
    static int countSteps(int arr[], int n)
    {
        int max = -1, steps = 0;
        for (int i = 0; i < n; i++) {
 
            // condition to find max
            if (arr[i] > max) {
 
                max = arr[i];
 
                // Count the number of times this
                // condition executes that will the
                // number of turns being played.
                steps++;
            }
        }
 
        // return the number of turns played
        return steps;
    }
 
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int arr[] = { 2, 5, 8, 24, 4, 11, 6,
                      1, 15, 10 };
        int n = arr.length;
 
        System.out.print("Steps Taken: ");
        System.out.println(countSteps(arr, n));
    }
}
// This code is contributed by Arnav Kr. Mandal.


Python 3
# Python program to simulate max deletion
# and calculate number of steps.
 
# Function to find the maximum number in the
# array upto index n and return its index.
def countSteps(arr, n):
    large, steps = -1, 0
 
    for i in range(n):
 
        # condition to find max
        if arr[i] > large:
            large = arr[i]
 
            # Count the number of times this
            # condition executes that will the
            # number of turns being played.
            steps += 1
 
    # return the number of turns played
    return steps
 
 
# Driver Code
if __name__ == "__main__":
    arr = [2, 5, 8, 24, 4, 11, 6, 1, 15, 10]
    n = len(arr)
 
    #'steps' variable calculates the number of
    # steps being taken.
    print("Steps Taken:", countSteps(arr, n))
 
# This code is contributed by
# sanjeev2552


C#
// C# code to simulate max
// deletion and calculate
// number of steps.
using System;
 
class GFG {
 
    // Function to find the maximum
    // number in the array upto index
    // n and return its index.
    static int countSteps(int[] arr, int n)
    {
        int max = -1, steps = 0;
        for (int i = 0; i < n; i++)
        {
 
            // condition to find max
            if (arr[i] > max) {
 
                max = arr[i];
 
                // Count the number of times
                // this condition executes
                // that will the number of
                // turns being played.
                steps++;
            }
        }
 
        // return the number of turns played
        return steps;
    }
 
    /* Driver program to test above function */
    public static void Main()
    {
        int[] arr = { 2, 5, 8, 24, 4, 11, 6,
                                 1, 15, 10 };
        int n = arr.Length;
 
        Console.Write("Steps Taken: ");
         
        Console.WriteLine(countSteps(arr, n));
    }
}
 
// This code is contributed by vt_m.


PHP
 $max)
        {
 
            $max = $arr[$i];
 
            // Count the number
            // of times this
            // condition executes
            // that will the number
            // of turns being played.
            $steps++;
        }
    }
 
    // return the number
    // of turns played
    return $steps;
}
 
    // Driver Code
    $arr = array(2, 5, 8, 24, 4,
                11, 6, 1, 15, 10);
    $n = count($arr);
 
    //'steps' variable calculates
    // the number of steps being
    // taken.
    echo "Steps Taken: ";
    echo countSteps($arr, $n);
     
// This code is contributed by anuj_67.
?>


Javascript


输出:

Steps Taken: 4

这种方法的时间复杂度是O(n^2)
有效的方法:
一种有效的方法是将最大值初始化为-1,并在计算数组中的最大值时计算交换条件执行的次数,即这个条件:

CPP

if (max < arr[i]) {
 
    // keep a count of number of times this
    // condition executes.
    max = arr[i];
}

每次交换发生时,都保证数组中的先前数字小于当前元素。这为我们提供了对于给定整数集将发生的确切步数。

C++

// C++ program to simulate max deletion
// and calculate number of steps.
#include 
using namespace std;
 
// Function to find the maximum number in the
// array upto index n and return its index.
int countSteps(int arr[], int n)
{
    int max = -1, steps = 0;
    for (int i = 0; i < n; i++) {
 
        // condition to find max
        if (arr[i] > max) {
 
            max = arr[i];
 
            // Count the number of times this
            // condition executes that will the
            // number of turns being played.
            steps++;
        }
    }
 
    // return the number of turns played
    return steps;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 5, 8, 24, 4, 11, 6, 1, 15, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    //'steps' variable calculates the number of
    // steps being taken.
    cout << "Steps Taken: ";
    cout << countSteps(arr, n) << endl;
    return 0;
}

Java

// Java code to simulate max deletion
// and calculate number of steps.
import java.util.*;
 
class GFG {
 
    // Function to find the maximum number in the
    // array upto index n and return its index.
    static int countSteps(int arr[], int n)
    {
        int max = -1, steps = 0;
        for (int i = 0; i < n; i++) {
 
            // condition to find max
            if (arr[i] > max) {
 
                max = arr[i];
 
                // Count the number of times this
                // condition executes that will the
                // number of turns being played.
                steps++;
            }
        }
 
        // return the number of turns played
        return steps;
    }
 
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int arr[] = { 2, 5, 8, 24, 4, 11, 6,
                      1, 15, 10 };
        int n = arr.length;
 
        System.out.print("Steps Taken: ");
        System.out.println(countSteps(arr, n));
    }
}
// This code is contributed by Arnav Kr. Mandal.

Python3

# Python program to simulate max deletion
# and calculate number of steps.
 
# Function to find the maximum number in the
# array upto index n and return its index.
def countSteps(arr, n):
    large, steps = -1, 0
 
    for i in range(n):
 
        # condition to find max
        if arr[i] > large:
            large = arr[i]
 
            # Count the number of times this
            # condition executes that will the
            # number of turns being played.
            steps += 1
 
    # return the number of turns played
    return steps
 
 
# Driver Code
if __name__ == "__main__":
    arr = [2, 5, 8, 24, 4, 11, 6, 1, 15, 10]
    n = len(arr)
 
    #'steps' variable calculates the number of
    # steps being taken.
    print("Steps Taken:", countSteps(arr, n))
 
# This code is contributed by
# sanjeev2552

C#

// C# code to simulate max
// deletion and calculate
// number of steps.
using System;
 
class GFG {
 
    // Function to find the maximum
    // number in the array upto index
    // n and return its index.
    static int countSteps(int[] arr, int n)
    {
        int max = -1, steps = 0;
        for (int i = 0; i < n; i++)
        {
 
            // condition to find max
            if (arr[i] > max) {
 
                max = arr[i];
 
                // Count the number of times
                // this condition executes
                // that will the number of
                // turns being played.
                steps++;
            }
        }
 
        // return the number of turns played
        return steps;
    }
 
    /* Driver program to test above function */
    public static void Main()
    {
        int[] arr = { 2, 5, 8, 24, 4, 11, 6,
                                 1, 15, 10 };
        int n = arr.Length;
 
        Console.Write("Steps Taken: ");
         
        Console.WriteLine(countSteps(arr, n));
    }
}
 
// This code is contributed by vt_m.

PHP

 $max)
        {
 
            $max = $arr[$i];
 
            // Count the number
            // of times this
            // condition executes
            // that will the number
            // of turns being played.
            $steps++;
        }
    }
 
    // return the number
    // of turns played
    return $steps;
}
 
    // Driver Code
    $arr = array(2, 5, 8, 24, 4,
                11, 6, 1, 15, 10);
    $n = count($arr);
 
    //'steps' variable calculates
    // the number of steps being
    // taken.
    echo "Steps Taken: ";
    echo countSteps($arr, $n);
     
// This code is contributed by anuj_67.
?>

Javascript


输出:

Steps Taken: 4