给定一个数组arr [],它表示一系列盒子堆叠,其中每个盒子的高度都等于1个单位。假设您位于第一个桩的顶部,则需要通过从最左端到最右端的每个桩头移动来到达地面。
限制条件:
- 当下一个堆的高度等于或小于其站立的堆的高度时,一个人可以从当前的箱子堆移动到下一个箱子。
- 人们还会遇到一些桩的高度大于其站立的桩的高度。因此,他们将需要从该堆中移走一些盒子才能前进。因此,任务是告诉在到达地面的过程中需要从每个堆中移走的盒子总数(如有必要)。
给出所有桩的高度。假设您站在第一堆上。打印要删除的盒子总数。
例子:
Input : arr[] = {3, 3, 2, 4, 1}
Output : 2
Explanation: After removing boxes, the heights of piles will be {3, 3, 2, 2, 1}
We are currently standing on 1st pile of height 3.
Step 1: We can move to the 2nd pile, since it’s height is equal to the height of the current pile.
Step 2: We can move to the 3rd pile of height 2, since it less than 3.
Step 3: We cannot go from 3rd pile to 4th pile(of height 4), so we need to remove 2 boxes from 4th pile to make it’s height equal to 2.
Step 4: We can easily move to the last pile since it’s height is 1 which is less than the height of the 4th pile of height 2(by removing 2 boxes in the previous step).
Input : arr[] = {5, 6, 7, 1}
Output : 3
Explanation : After removing boxes, the heights of piles will be {5, 5, 5, 1}
We are currently standing on 1st pile of height 5.
Step 1: We cannot move to the 2nd pile since it’s height is more. So, we remove 1 box and make its height equal to 5 and then we move forward.
Step 2: We cannot move to the 3rd pile of height 7, so we remove 2 boxes from it.
Step 3: We can easily move to the last pile since it’s height is 1 which is less than the height of the 3rd pile of height 5.
想法是从左开始遍历数组,然后每次向前移动之前,将当前桩的高度与前一桩的高度进行比较。如果当前桩的高度大于先前桩的高度,则以两个高度之差递增计数,否则在数组中向前移动。
下面是上述方法的实现:
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是桩的总数。