《剑指offer》刷题——【动态规划与贪婪算法】面试题14:剪绳子(java实现)

《剑指offer》刷题——【动态规划与贪婪算法】面试题14:剪绳子

  • 一、动态规划
    • 1. 应用动态规划求解的问题的特点
  • 二、贪婪算法
  • 三、题目描述
  • 四、解题思路
    • (一)动态规划
      • 1. 思路分析
      • 2. 代码实现(动态规划)
      • 3. 时间复杂度O(n^2)
    • (二)贪婪算法
      • 1. 思路分析
      • 2. 代码实现
      • 3. 时间复杂度O(1)

一、动态规划

1. 应用动态规划求解的问题的特点

  • 求一个问题的最优解(最大值、最小值)
  • 该问题能否分解成若干个子问题
  • 子问题之间还有重叠的更小的子问题
  • 从上往下分析问题,从下往上求解问题

二、贪婪算法

  • 每一步都可以做出一个贪婪的选择,基于这个选择,我们确定能够得到最优解

三、题目描述

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

四、解题思路

(一)动态规划

1. 思路分析

  • 首先定义函数 f(n) 为把长度为n的绳子剪成若干段后各段长度乘积的最大值
  • 在剪第一刀的时候,我们有 n-1中可能的选择,即剪出来的一段绳子的可能长度分别为1,2,。。。n-1
  • f(n) = max( f(i) f(n-i))
  • 递归存在很多重复的子问题,从而有大量不必要的重复计算,因此从下而上计算
  • 例如:先求f(2) f(3) ,再得到 f(4) f(5),直到f(n)
  • 当绳子的长度为2时,只能剪成长度都为1的两段, f(2) = 1;
  • 当绳子的长度为3时,可以剪成1和2,或者三个1, f(3) = 2

2. 代码实现(动态规划)

/**
 * @author hw
 * @version 0.1
 * @description
 * @date 2019/4/27
 */
public class cutCord {
    public int maxProductAferCutting_solution(int length){
        if (length<2)
            return 1;
        if (length==2)
            return 1;
        if (length==3)
            return 2;

        //存储子问题的最优解
        //数组中第i个元素表示把长度为i的绳子剪成若干段之后各段长度乘积的最大值
        int[] products = new int[length+1];
        products[0]=0;
        products[1]=1;
        products[2]=2;
        products[3]=3;

        int max = 0;
        //自下而上
        //因此再求f(i)之前,对于每一个j(0
        for (int i = 4; i <= length; i++) {
            max = 0;
            //为了求解f(i),需要求出所有可能的f(j) x f(i-j),并比较他们的最大值
            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];
        return max;
    }

    public static void main(String[] args) {
        cutCord cord = new cutCord();
        int max = cord.maxProductAferCutting_solution(8);
        System.out.println("最大乘积:" + max);
    }

}

3. 时间复杂度O(n^2)

(二)贪婪算法

1. 思路分析

  • 当 n>=5时,尽可能多的剪长度为3的绳子;
  • 当剩下的长度为4,把绳子剪成两段为2

2. 代码实现

import static java.lang.Math.pow;

/**
 * @author hw
 * @version 0.1
 * @description
 * @date 2019/4/27
 */
public class cutCord2 {
    public int maxProductAferCutting_solution2(int length) {
        if (length < 2)
            return 1;
        if (length == 2)
            return 1;
        if (length == 3)
            return 2;

        //尽可能多地剪去长度为3的绳子段
        int timeOf3 = length/3;
        //当绳子最后剩下的长度为4时,不能再剪去长度为3的绳子段
        //此时更好把绳子剪成长度为2的两段,因为 2x2 > 3x1
        if (length-timeOf3*3 ==1){
            timeOf3 -= 1;
        }
        int timeOf2 = (length-timeOf3*3)/2;
        //pow 第一个参数的值的第二个参数次幂
        return (int)(pow(3,timeOf3))*(int)(pow(2,timeOf2));
    }


    public static void main(String[] args) {
        cutCord2 cord = new cutCord2();
        int max = cord.maxProductAferCutting_solution2(7);
        System.out.println("最大乘积:" + max);
    }

}

3. 时间复杂度O(1)

你可能感兴趣的:(学习笔记,剑指offer)