题目:给你一根长度为n的绳子,请把绳子剪成m段(m,n都是整数,n > 1 并且m > 1),每段绳子的长度记为k[0], k[1], ...k[m]。请问k[0] x k[1] x ... x k[m]可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。

测试用例:

  • 功能测试(绳子的初始长度大于5)。
  • 边界值测试(绳子的初始长度分别为0、1、2、3、4)。

测试代码:

void test(const char* testName, int length, int expected)
{
    int result1 = maxProductAfterCutting_solution1(length);
    if(result1 == expected)
        std::cout << "Solution1 for " << testName << " passed." << std::endl;
    else
        std::cout << "Solution1 for " << testName << " FAILED." << std::endl;

    int result2 = maxProductAfterCutting_solution2(length);
    if(result2 == expected)
        std::cout << "Solution2 for " << testName << " passed." << std::endl;
    else
        std::cout << "Solution2 for " << testName << " FAILED." << std::endl;
}
void test1()
{
    int length = 1;
    int expected = 0;
    test("test1", length, expected);
}
void test2()
{
    int length = 2;
    int expected = 1;
    test("test2", length, expected);
}
void test3()
{
    int length = 3;
    int expected = 2;
    test("test3", length, expected);
}
void test4()
{
    int length = 4;
    int expected = 4;
    test("test4", length, expected);
}
void test5()
{
    int length = 5;
    int expected = 6;
    test("test5", length, expected);
}
void test6()
{
    int length = 6;
    int expected = 9;
    test("test6", length, expected);
}
void test7()
{
    int length = 7;
    int expected = 12;
    test("test7", length, expected);
}
void test8()
{
    int length = 8;
    int expected = 18;
    test("test8", length, expected);
}
void test9()
{
    int length = 9;
    int expected = 27;
    test("test9", length, expected);
}
void test10()
{
    int length = 10;
    int expected = 36;
    test("test10", length, expected);
}
void test11()
{
    int length = 50;
    int expected = 86093442;
    test("test11", length, expected);
}

本题考点:

  • 考查应聘者的抽象建模能力。应聘者需要把一个具体的场景抽象成一个能够用动态规划或者贪婪算法解决的模型。
  • 考查应聘者对动态规划和贪婪算法的理解。能够灵活运用动态规划解决问题的关键是具备从上到下分析问题、从下到上解决问题的能力,而灵活运用贪婪算法则需要扎实的数学基本功。

动态规划:

int maxProductAfterCutting_solution1(int length)
{
    if(length < 2)
        return 0;
    if(length == 2)
        return 1;
    if(length == 3)
        return 2;
    int* products = new int[length + 1];
    products[0] = 0;
    products[1] = 1;
    products[2] = 2;
    products[3] = 3;
    int max = 0;
    for(int i = 4; i <= length; ++i)
    {
        max = 0;
        for(int j = 1; j <= i / 2; ++j)
        {
            int product = products[j] * products[i - j];
            if(max < product)
                max = product;

            products[i] = max;
        }
    }
    max = products[length];
    delete[] products;
    return max;
}

贪婪算法:

int maxProductAfterCutting_solution2(int length)
{
    if(length < 2)
        return 0;
    if(length == 2)
        return 1;
    if(length == 3)
        return 2;
    // 尽可能多地减去长度为3的绳子段
    int timesOf3 = length / 3;
    // 当绳子最后剩下的长度为4的时候,不能再剪去长度为3的绳子段。
    // 此时更好的方法是把绳子剪成长度为2的两段,因为2*2 > 3*1。
    if(length - timesOf3 * 3 == 1)
        timesOf3 -= 1;
    int timesOf2 = (length - timesOf3 * 3) / 2;
    return (int) (pow(3, timesOf3)) * (int) (pow(2, timesOf2));
}
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄