📜  选择除法或按原样考虑的最大值

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

我们得到一个数字 n,我们需要在以下函数的帮助下找到可能的最大和:
F(n) = max( (F(n/2) + F(n/3) + F(n/4) + F(n/5)), n) 。为了计算 F(n, ),我们可以将 n 作为我们的结果,或者我们可以像给定的函数定义那样将 n 进一步分成四部分。这可以尽可能多地完成。找出您可以从给定 N 中获得的最大可能总和。 注意:1 不能进一步分解,因此 F(1) = 1 作为基本情况。

例子 :

Input : n = 10
Output : MaxSum = 12
Explanation: 
f(10) = f(10/2) + f(10/3) + f(10/4) + f(10/5)
      = f(5)  +   f(3)  +  f(2)   +  f(2)
      = 12
5, 3 and 2 cannot be further divided.

Input : n = 2
Output : MaxSum = 2

方法:这个问题可以用递归方法解决,但由于其重叠的子问题,这将给我们带来很高的复杂性。所以我们应用动态规划的概念以自下而上的方式解决这个问题:

C++
// CPP program for maximize result when
// we have choice to divide or consider
// as it is.
#include 
using namespace std;
 
// function for calculating max possible result
int maxDP(int n)
{
    int res[n + 1];
    res[0] = 0;
    res[1] = 1;
 
    // Compute remaining values in bottom
    // up manner.
    for (int i = 2; i <= n; i++)
        res[i] = max(i, (res[i / 2]
                         + res[i / 3]
                         + res[i / 4]
                         + res[i / 5]));
 
    return res[n];
}
 
// Driver Code
int main()
{
    int n = 60;
    cout << "MaxSum =" << maxDP(n);
    return 0;
}


Java
// Java program for maximize result when
// we have choice to divide or consider
// as it is.
import java.io.*;
 
class GFG {
 
    // function for calculating max
    // possible result
    static int maxDP(int n)
    {
        int res[] = new int[n + 1];
        res[0] = 0;
        res[1] = 1;
 
        // Compute remaining values in
        // bottom up manner.
        for (int i = 2; i <= n; i++)
            res[i]
                = Math.max(i, (res[i / 2]
                               + res[i / 3]
                               + res[i / 4]
                               + res[i / 5]));
 
        return res[n];
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 60;
        System.out.println("MaxSum = " + maxDP(n));
    }
}
 
// This code is contributed by vt_m


Python3
# Python3 code to maximize result when
# we have choice to divide or consider
# as it is.
 
# function for calculating max
# possible result
def maxDP (n):
    res = list()
    res.append(0)
    res.append(1)
     
    # Compute remaining values in
    # bottom up manner.
    i = 2
    while i


C#
// C# program for maximize result when
// we have choice to divide or consider
// as it is.
using System;
 
class GFG {
 
    // function for calculating max
    // possible result
    static int maxDP(int n)
    {
        int[] res = new int[n + 1];
        res[0] = 0;
        res[1] = 1;
 
        // Compute remaining values in
        // bottom up manner.
        for (int i = 2; i <= n; i++)
            res[i] = Math.Max(i, (res[i / 2]
                                  + res[i / 3]
                                  + res[i / 4]
                                  + res[i / 5]));
 
        return res[n];
    }
 
    // Driver code
    public static void Main()
    {
        int n = 60;
        Console.WriteLine("MaxSum = " + maxDP(n));
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


输出
MaxSum =106

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

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