📌  相关文章
📜  浇灌所有植物所需的操作次数

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

浇灌所有植物所需的操作次数

给定一个包含N个整数的数组arr[] ,其中第 i元素表示植物在第 i索引处所需的水量和一个整数K ,任务是计算使用一个容器为所有植物浇水所需的操作次数最多可容纳K升水,其中每次操作,

  • 您可以移动到左侧或右侧的相邻植物。
  • 此外,在索引-1处有一条河流,可以从那里多次重新填充容器。
  • 请注意,最初,在索引-1处,任何植物都不能在任何步骤中部分浇水。

例子:

方法:给定的问题是一个基于实现的问题。可以通过以下步骤解决:

  • 创建一个变量current_capacity来存储容器中当前的水量。最初, current_capacity = K
  • 使用变量i遍历给定数组arr[]并执行以下操作:
    • 如果arr[i] > K ,则返回-1
    • 如果arr[i] > current_capacity ,将2*i + 1添加到操作计数中并设置current_capacity = K – arr[i]
    • 否则,将操作计数加1并设置current_capacity = current_capacity – arr[i]

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
 
// Function to find count of operation
// required to water all the plants
int reqOperationCnt(vector& arr, int K)
{
 
    // Stores current capacity
    // of the container
    int current_capacity = K;
 
    // Stores the final count
    int cnt = 0;
 
    // Loop to traverse arr[]
    for (int i = 0; i < arr.size(); i++) {
 
        // If required water is
        // more than max capacity
        if (arr[i] > K) {
            return -1;
        }
 
        // If container does not
        // have enough water
        if (current_capacity < arr[i]) {
 
            // Update cnt
            cnt += 2 * i + 1;
 
            // Update current capacity
            // to the remaining water
            current_capacity = K - arr[i];
        }
        else {
 
            // Update current capacity
            cnt++;
            current_capacity -= arr[i];
        }
    }
 
    // Return Answer
    return cnt;
}
 
// Driver Code
int main()
{
 
    vector arr{ 2, 2, 3, 3 };
    int K = 5;
    cout << reqOperationCnt(arr, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
public class GFG {
 
  // Function to find count of operation
  // required to water all the plants
  static int reqOperationCnt(int []arr, int K)
  {
 
    // Stores current capacity
    // of the container
    int current_capacity = K;
 
    // Stores the final count
    int cnt = 0;
 
    // Loop to traverse arr[]
    for (int i = 0; i < arr.length; i++) {
 
      // If required water is
      // more than max capacity
      if (arr[i] > K) {
        return -1;
      }
 
      // If container does not
      // have enough water
      if (current_capacity < arr[i]) {
 
        // Update cnt
        cnt += 2 * i + 1;
 
        // Update current capacity
        // to the remaining water
        current_capacity = K - arr[i];
      }
      else {
 
        // Update current capacity
        cnt++;
        current_capacity -= arr[i];
      }
    }
 
    // Return Answer
    return cnt;
  }
 
  // Driver code
  public static void main (String args[]) {
    int []arr = { 2, 2, 3, 3 };
    int K = 5;
    System.out.println(reqOperationCnt(arr, K));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


C#
// C# program for the above approach
using System;
class GFG {
 
  // Function to find count of operation
  // required to water all the plants
  static int reqOperationCnt(int []arr, int K)
  {
 
    // Stores current capacity
    // of the container
    int current_capacity = K;
 
    // Stores the final count
    int cnt = 0;
 
    // Loop to traverse arr[]
    for (int i = 0; i < arr.Length; i++) {
 
      // If required water is
      // more than max capacity
      if (arr[i] > K) {
        return -1;
      }
 
      // If container does not
      // have enough water
      if (current_capacity < arr[i]) {
 
        // Update cnt
        cnt += 2 * i + 1;
 
        // Update current capacity
        // to the remaining water
        current_capacity = K - arr[i];
      }
      else {
 
        // Update current capacity
        cnt++;
        current_capacity -= arr[i];
      }
    }
 
    // Return Answer
    return cnt;
  }
 
  // Driver code
  public static void Main () {
    int []arr = { 2, 2, 3, 3 };
    int K = 5;
    Console.Write(reqOperationCnt(arr, K));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python code for the above approach
 
# Function to find count of operation
# required to water all the plants
def reqOperationCnt(arr, K):
 
    # Stores current capacity
    # of the container
    current_capacity = K
 
    # Stores the final count
    cnt = 0
 
    # Loop to traverse arr[]
    for i in range(len(arr)):
 
        # If required water is
        # more than max capacity
        if (arr[i] > K):
            return -1
 
        # If container does not
        # have enough water
        if (current_capacity < arr[i]):
 
            # Update cnt
            cnt += 2 * i + 1
 
            # Update current capacity
            # to the remaining water
            current_capacity = K - arr[i]
 
        else:
 
            # Update current capacity
            cnt += 1
            current_capacity -= arr[i]
 
    # Return Answer
    return cnt
 
# Driver Code
arr = [2, 2, 3, 3]
K = 5
print(reqOperationCnt(arr, K))
 
# This code is contributed by Saurabh Jaiswal


Javascript


输出
14

时间复杂度: O(N)
辅助空间: O(1)