📌  相关文章
📜  找到要移除的盒子数量

📅  最后修改于: 2021-04-29 14:24:08             🧑  作者: Mango

给定一个数组arr [],它表示一系列盒子堆叠,其中每个盒子的高度都等于1个单位。假设您位于第一个桩的顶部,则需要通过从最左端到最右端的每个桩头移动来到达地面。

限制条件

  • 当下一个堆的高度等于或小于其站立的堆的高度时,一个人可以从当前的箱子堆移动到下一个箱子。
  • 人们还会遇到一些桩的高度大于其站立的桩的高度。因此,他们将需要从该堆中移走一些盒子才能前进。因此,任务是告诉在到达地面的过程中需要从每个堆中移走的盒子总数(如有必要)。

给出所有桩的高度。假设您站在第一堆上。打印要删除的盒子总数。

例子

想法是从左开始遍历数组,然后每次向前移动之前,将当前桩的高度与前一桩的高度进行比较。如果当前桩的高度大于先前桩的高度,则以两个高度之差递增计数,否则在数组中向前移动。

下面是上述方法的实现:

C++
// C++ program to find the number of
// boxes to be removed
#include 
using namespace std;
  
// Function to find the number of
// boxes to be removed
int totalBoxesRemoved(int arr[], int n)
{
    int count = 0;
  
    // Store height of previous pile
    int prev = arr[0];
  
    // Start traversing the array
    for (int i = 1; i < n; i++) {
        // if height of current pile is greater
        // than previous pile
        if (arr[i] > prev) {
            // Increment count by difference
            // of two heights
            count += (arr[i] - prev);
  
            // Update current height
            arr[i] = prev;
  
            // Update prev for next iteration
            prev = arr[i];
        }
        else {
            // Update prev for next iteration
            prev = arr[i];
        }
    }
  
    return count;
}
  
// Driver code
int main()
{
    int arr[] = { 5, 4, 7, 3, 2, 1 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << totalBoxesRemoved(arr, n);
  
    return 0;
}


Java
// Java program to find the number of
// boxes to be removed
import java.io.*;
  
class GFG {
  
// Function to find the number of
// boxes to be removed
static int totalBoxesRemoved(int arr[], int n)
{
    int count = 0;
  
    // Store height of previous pile
    int prev = arr[0];
  
    // Start traversing the array
    for (int i = 1; i < n; i++) {
        // if height of current pile is greater
        // than previous pile
        if (arr[i] > prev) {
            // Increment count by difference
            // of two heights
            count += (arr[i] - prev);
  
            // Update current height
            arr[i] = prev;
  
            // Update prev for next iteration
            prev = arr[i];
        }
        else {
            // Update prev for next iteration
            prev = arr[i];
        }
    }
  
    return count;
}
  
    // Driver code
    public static void main (String[] args) {
            int arr[] = { 5, 4, 7, 3, 2, 1 };
  
    int n = arr.length;
  
    System.out.println(totalBoxesRemoved(arr, n));
    }
}
  
// This code is contributed 
// by inder_verma..


Python3
# Python3 program to find the 
# number of boxes to be removed 
  
# Function to find the number
# of boxes to be removed 
def totalBoxesRemoved(arr, n): 
  
    count = 0
      
    # Store height of previous pile 
    prev = arr[0] 
  
    # Start traversing the array 
    for i in range(1, n):
          
        # if height of current pile 
        # is greater than previous pile 
        if (arr[i] > prev) : 
              
            # Increment count by 
            # difference of two heights 
            count += (arr[i] - prev) 
  
            # Update current height 
            arr[i] = prev 
  
            # Update prev for next 
            # iteration 
            prev = arr[i] 
          
        else :
            # Update prev for next 
            # iteration 
            prev = arr[i] 
          
    return count
  
# Driver code 
arr = [ 5, 4, 7, 3, 2, 1 ] 
  
n = len(arr) 
  
print(totalBoxesRemoved(arr, n)) 
  
# This code is contributed 
# by Yatin Gupta


C#
// C# program to find the number of
// boxes to be removed
using System;
  
class GFG {
  
// Function to find the number of
// boxes to be removed
static int totalBoxesRemoved(int []arr, int n)
{
    int count = 0;
  
    // Store height of previous pile
    int prev = arr[0];
  
    // Start traversing the array
    for (int i = 1; i < n; i++) {
        // if height of current pile is greater
        // than previous pile
        if (arr[i] > prev) {
            // Increment count by difference
            // of two heights
            count += (arr[i] - prev);
  
            // Update current height
            arr[i] = prev;
  
            // Update prev for next iteration
            prev = arr[i];
        }
        else {
            // Update prev for next iteration
            prev = arr[i];
        }
    }
  
    return count;
}
  
        // Driver code
    public static void Main () {
            int []arr = { 5, 4, 7, 3, 2, 1 };
  
    int n = arr.Length;
  
    Console.WriteLine(totalBoxesRemoved(arr, n));
    }
}
  
// This code is contributed 
// by shs


PHP
 $prev) 
        {
            // Increment count by difference
            // of two heights
            $count += ($arr[$i] - $prev);
  
            // Update current height
            $arr[$i] = $prev;
  
            // Update prev for next iteration
            $prev = $arr[$i];
        }
        else 
        {
            // Update prev for next iteration
            $prev = $arr[$i];
        }
    }
  
    return $count;
}
  
// Driver code
$arr = array( 5, 4, 7, 3, 2, 1 );
  
$n = count($arr);
  
echo totalBoxesRemoved($arr, $n);
  
// This code is contributed 
// by shs
?>


输出:
3

时间复杂度:O(N),其中N是桩的总数。