📜  使数组不增加所需的最少对合并操作

📅  最后修改于: 2021-09-22 10:29:01             🧑  作者: Mango

给定一个数组A[] ,任务是找到从数组中删除两个相邻元素并用它们的总和替换所需的最小操作次数,以便将数组转换为非递增数组。

注意:具有单个元素的数组被认为是非递增的。

例子:

方法:想法是使用动态规划。记忆表用于存储使子数组从给定数组的右到左不增加所需的最小操作计数。请按照以下步骤解决问题:

  • 初始化数组dp[] ,其中dp[i]存储使子数组 {A[i], …, A[N]} 不增加所需的最小操作数。因此,目标是计算 dp[0]。
  • 找到一个最小子数组{A[i] .. A[j]}使得sum({A[i] … A[j]}) > val[j+1] ,其中, val[j + 1] 是为子数组{A[j + 1], … A[N]}获得的合并总和。
  • dp[i]更新为j – i + dp[j+1]并将vals[i] 更新sum({A[i] … A[j]})

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the minimum operations
// to make the array Non-increasing
int solve(vector& a)
{
 
    // Size of the array
    int n = a.size();
 
    // Dp table initialization
    vector dp(n + 1, 0), val(n + 1, 0);
 
    // dp[i]: Stores minimum number of
    // operations required to make
    // subarray {A[i], ..., A[N]} non-increasing
    for (int i = n - 1; i >= 0; i--) {
        long long sum = a[i];
        int j = i;
 
        while (j + 1 < n and sum < val[j + 1]) {
 
            // Increment the value of j
            j++;
 
            // Add current value to sum
            sum += a[j];
        }
 
        // Update the dp tables
        dp[i] = (j - i) + dp[j + 1];
        val[i] = sum;
    }
 
    // Return the answer
    return dp[0];
}
 
// Driver code
int main()
{
    vector arr = { 1, 5, 3, 9, 1 };
    cout << solve(arr);
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to find the minimum operations
// to make the array Non-increasing
static int solve(int []a)
{
 
    // Size of the array
    int n = a.length;
 
    // Dp table initialization
    int []dp = new int[n + 1];
    int []val = new int[n + 1];
 
    // dp[i]: Stores minimum number of
    // operations required to make
    // subarray {A[i], ..., A[N]} non-increasing
    for (int i = n - 1; i >= 0; i--)
    {
        int sum = a[i];
        int j = i;
 
        while (j + 1 < n && sum < val[j + 1])
        {
 
            // Increment the value of j
            j++;
 
            // Add current value to sum
            sum += a[j];
        }
 
        // Update the dp tables
        dp[i] = (j - i) + dp[j + 1];
        val[i] = sum;
    }
 
    // Return the answer
    return dp[0];
}
 
// Driver code
public static void main(String[] args)
{
    int []arr = { 1, 5, 3, 9, 1 };
    System.out.print(solve(arr));
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to implement
# the above approach
 
# Function to find the minimum operations
# to make the array Non-increasing
def solve(a):
 
    # Size of the array
    n = len(a)
 
    # Dp table initialization
    dp = [0] * (n + 1)
    val = [0] * (n + 1)
 
    # dp[i]: Stores minimum number of
    # operations required to make
    # subarray {A[i], ..., A[N]} non-increasing
    for i in range(n - 1, -1, -1):
        sum = a[i]
        j = i
 
        while(j + 1 < n and sum < val[j + 1]):
 
            # Increment the value of j
            j += 1
 
            # Add current value to sum
            sum += a[j]
 
        # Update the dp tables
        dp[i] = (j - i) + dp[j + 1]
        val[i] = sum
 
    # Return the answer
    return dp[0]
 
# Driver Code
arr = [ 1, 5, 3, 9, 1 ]
 
# Function call
print(solve(arr))
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to find the minimum operations
// to make the array Non-increasing
static int solve(int []a)
{
 
    // Size of the array
    int n = a.Length;
 
    // Dp table initialization
    int []dp = new int[n + 1];
    int []val = new int[n + 1];
 
    // dp[i]: Stores minimum number of
    // operations required to make
    // subarray {A[i], ..., A[N]} non-increasing
    for (int i = n - 1; i >= 0; i--)
    {
        int sum = a[i];
        int j = i;
 
        while (j + 1 < n && sum < val[j + 1])
        {
 
            // Increment the value of j
            j++;
 
            // Add current value to sum
            sum += a[j];
        }
 
        // Update the dp tables
        dp[i] = (j - i) + dp[j + 1];
        val[i] = sum;
    }
 
    // Return the answer
    return dp[0];
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 1, 5, 3, 9, 1 };
    Console.Write(solve(arr));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程