动态规划-货币问题

动态规划-货币问题

题目一

arr是货币数组,其中的值都是正数。再给定一个正数aim。每个值都认为是一张货币,即便是值相同的货币也认为每一张都是不同的,返回组成aim的方法数。例如 : arr = { 1,1,1 },aim = 2,第0个和第1个能组成2,第1个和第2个能组成2,第0个和第2个能组成2,一共3种方法,所以返回3

从递归入手,对于每一个货币,要么选,要么不选,那么递归代码如下:

cpp 复制代码
int MethodCount1(vector<int>& arr, int aim) {
    return process1(arr, aim, 0, 0);//从0位置开始的总方法数
}
int process1(vector<int>& arr, int aim, int curIndex, int total) {
    if (total == aim) {
        return 1;
    }
    if (total > aim || curIndex == arr.size()) {
        return 0;
    }
    //1.选择当前位置
    int p1 = process1(arr, aim, curIndex + 1, total + arr[curIndex]);
    //2.不选
    int p2 = process1(arr, aim, curIndex + 1, total);
    return p1 + p2;
}

其中process1的参数含义如下:

  • curIndex:表示当前正在arr的curIndex位置做决策(是否选择curIndex位置的货币)
  • total:表示在curIndex之前做的决策已经积累了多少货币

递归终止条件:

  • total==aim,表示当前来到curIndex位置是,前面做的决策组成的总货币数量正好等于title,可以直接返回1,表示前面做的决策是正确的,同时后面不应该继续决策(否则total会大于aim)
  • total>aim,表示前面做的决策是错误的,返回0种方法
  • total<aim,但是curIndex到达末尾位置,表示前面做的决策不能凑到aim的货币,返回0

当来到某一个位置时,可以选择使用当前位置的货币,对应p1,也可以选择不使用当前位置的货币,对应p2,然后在到下一个为止去做选择,这是2种不同的选择,所以总的方法数为p1+p2

递归过程中是否存在重复子过程?

具有重复子过程的递归才有改动态规划的意义,上面的递归过程是可能存在重复子过程的,以[1,1,1,2,5,3],aim=7为例,可以选择下标为0和下标为1位置的1,不选择下标为2位置的1,于是就来到了下标为3的位置,此时total=2;也可以选择下标为1和下标为2位置的1,不选择下标为0位置的1,也来到了下标为3的位置,total=2,来到下标为3的位置,total=2出现了重复,因此上述递归过程存在重复子过程,改动态规划有利可图

动态规划

  1. 分析可变参数的变化范围,在递归过程中,可变参数只有2个,curIndex和total,curIndex的变化范围是[0,arr.size()],total的变化范围是[0,aim],在递归过程中,虽然可能存在total>aim的情况,但是此时的返回值是确定的,为0,可以在改动态规划时进行边界判断,因此dp表的大小为arr.size()*aim

  2. 分析位置依赖,从递归过程可以看出,process1(curIndex,total)依赖于process1(curIndex + 1, total)process1(curIndex + 1, total + arr[curIndex]),反应到dp表中就是当前行只依赖于下一行的数据,因此在填表时从最后一行开始填即可

  3. 状态转移方程,递归的过程就是状态转移方程,process1(curIndex,total)依赖于process1(curIndex + 1, total)process1(curIndex + 1, total + arr[curIndex]),所以状态转移方程就是dp[i][j]=dp[i+1][j]+dp[i+1][j+arr[i]]

  4. 初始化,dp表的初始化就是递归过程的临界条件,当total==aim的时候,无论curIndex为和值,结果都是1,当total!=aim,total > aim || curIndex == arr.size()时,结果是0

  5. 动态规划代码:

    cpp 复制代码
    int MethodCount1dp(vector<int>& arr, int aim) {
        //可变参数为curIndex和total
        //curIndex->[0,arr.size()]
        //total->[0,aim],因为当total>aim时返回0
        vector<vector<int>> dp(arr.size() + 1, vector<int>(aim + 1));
        for (int curIndex = 0; curIndex <= arr.size(); curIndex++) {
            dp[curIndex][aim] = 1;//total==aim时
        }
        for (int curIndex = arr.size() - 1; curIndex >= 0; curIndex--) {//从最后一行开始填表
            for (int total = 0; total < aim; total++) {
                //total从左往后或从右往左填无所谓
                //循环条件为total<aim,因为total==aim时结果为1
                int p1 = total + arr[curIndex] > aim ? 0 : dp[curIndex + 1][total + arr[curIndex]];//对total + arr[curIndex]>aim的情况进行特殊处理,根据递归郭晨可知,大于aim时结果为0
                int p2 = dp[curIndex + 1][total];
                dp[curIndex][total] = p1 + p2;
            }
        }
        return dp[0][0];//主函数调用process1(arr, aim, 0, 0),因此返回dp[0][0]
    }

题目二

arr是面值数组,其中的值都是正数且没有重复。再给定一个正数aim。每个值都认为是一种面值,且认为张数是无限的。返回组成aim的方法数例如︰arr = {1,2}, aim = 4。方法如下∶1 + 1 + 1 + 1、1 + 1 + 2、2 + 2一共3种方法,所以返回3。

递归过程与题目1类似,均为从左往右的尝试模型,只是每一种面值都可以选择无限多张,在递归时需要注意选择的总面额要<=aim,递归过程:

cpp 复制代码
int MethodCount2(vector<int>& arr, int aim) {
    //暴力递归:每一个位置可以选择多次
    return process2(arr, 0, aim);//当前在0位置,还需要凑齐aim元的方法
}
int process2(vector<int>& arr, int Index, int rest) {
    if (rest == 0) {
        return 1;
    }
    if (Index == arr.size()) {//不会出现aim<0的情况,因为在for循环中已经避免
        return 0;
    }
    int ans = 0;
    //当前位置可以选很多次
    for (int cnt = 0; cnt * arr[Index] <= rest; cnt++) {
        ans += process2(arr, Index + 1, rest - (cnt * arr[Index]));
    }
    return ans;
}

参数含义:

  • Index,表示当前正在Index位置做决策
  • rest,还需要凑总计rest价值的面额,当rest==0时表示不需要凑面额了,也就是找到了一种方法。第二个参数也可以定义为目前已经凑到的面额数,这样就与题目1一样,只要逻辑上合理即可

递归过程:

  • 当rest==0时,表示还需要凑0价值的面额,表示前面所做的决策是正确的,找到了1种方法
  • rest>0&&Index == arr.size(),表示已经达到结尾,但是还有rest面额需要去凑,此时返回0
  • rest>0&&Index < arr.size(),此时可以选择自由选择Index位置的面额,可以选1次、2次、......,不过需要注意的是,选择n次之后rest应该保证大于0,例如需要凑5元的面值,不能选6张1块的,通过cnt * arr[Index] <= rest可以保证这一点
  • 递归过程中不可能出现rest<0的情况,因为主函数在调用process2时传入的rest一定大于0,在process2中,for循环内调用process2也保证了rest>=0,因此整个过程rest>=0

动态规划

在process2过程中,process2(Index,rest)依赖于process2(Index+1,......),当前行只依赖于下一行的数据,因此在填dp表时,从最后一行开始填

cpp 复制代码
int MethodCount2dp(vector<int>& arr, int aim) {
    //Index->[0,arr.size()]
    //rest->[0,aim]
    vector<vector<int>> dp(arr.size() + 1, vector<int>(aim + 1));
    for (int i = 0; i <= arr.size(); i++) {
        dp[i][0] = 1;//rest==0,return 1
    }
    for (int Index = arr.size() - 1; Index >= 0; Index--) {
        for (int rest = 1; rest <= aim; rest++) {//rest==0已经填过了
            int ans = 0;
            for (int cnt = 0; cnt * arr[Index] <= rest; cnt++) {
                ans += dp[Index + 1][rest - (cnt * arr[Index])];
            }
            dp[Index][rest] = ans;
        }
    }
    return dp[0][aim];
}

分析位置依赖

在动态规划方法的填表过程中,确定一个位置需要通过for循环完成,我们可以画出位置依赖图

假设rest=7,当前来到2号下标做选择,arr[2]=3,那么根据for循环代码,(rest,Index)位置依赖于下一行的图示位置A、B和C,同时还可以观察出(rest-arr[Index],Index)依赖于下一行的A和B,所以一个位置实际上依赖于它左边的位置和它下方的位置,因此动态规划的状态转移方程可以简化为dp[Index][rest]=dp[Index+1][rest]+dp[Index][rest-arr[Index]](需要处理边界),这是通过分析位置依赖得出的结论。所以上面的代码可以改写为:

cpp 复制代码
int MethodCount2dp(vector<int>& arr, int aim) {
    vector<vector<int>> dp(arr.size() + 1, vector<int>(aim + 1));
    for (int i = 0; i <= arr.size(); i++) {
        dp[i][0] = 1;
    }
    for (int Index = arr.size() - 1; Index >= 0; Index--) {
        for (int rest = 1; rest <= aim; rest++) {//rest==0已经填过了
            dp[Index][rest]=dp[Index+1][rest];
            if(rest-arr[Index]>=0){
                dp[Index][rest]+=dp[Index][rest-arr[Index]];
            }
        }
    }
    return dp[0][aim];
}

题目三

arr是货币数组,其中的值都是正数。再给定一个正数aim。每个值都认为是一张货币,认为值相同的货币没有任何不同,返回组成aim的方法数。例如 : arr = {1,2,1,1,2,1,2}, aim = 4,方法∶1 + 1 + 1 + 1、1 + 1 + 2、2 + 2一共就3种方法,所以返回3

题目3中需要统计出每种面值出现的次数,递归过程也是采用从左往右进行尝试

cpp 复制代码
int MethodCount3(vector<int>& arr, int aim) {
    vector<int> counts;//统计张数
    vector<int> money;
    unordered_map<int, int> hashMap;
    for (int m : arr) {
        hashMap[m]++;
    }
    for (auto [m, c] : hashMap) {
        money.push_back(m);
        counts.push_back(c);
    }
    return process3(money, counts, 0, aim);
}
int process3(vector<int>& money, vector<int>& counts, int Index, int rest) {
    if (rest == 0) {
        return 1;
    }
    if (Index == money.size()) {
        return 0;//rest>0
    }
    int ans = 0;
    for (int cnt = 0; cnt <= counts[Index] && rest - cnt * money[Index] >= 0; cnt++) {
        ans += process3(money, counts, Index + 1, rest - cnt * money[Index]);
    }
    return ans;
}

与题目2不同的是,题目3中的for循环除了保证rest减完之后大于0,还要保证在Index位置使用的货币数量不能超过原数组中对应的面值数量。

动态规划

由递归过程可以比较容易的改出动态规划版本

cpp 复制代码
int MethodCount3dp(vector<int>& arr, int aim) {
    vector<int> counts;
    vector<int> money;
    unordered_map<int, int> hashMap;
    for (int m : arr) {
        hashMap[m]++;
    }
    for (auto [m, c] : hashMap) {
        money.push_back(m);
        counts.push_back(c);
    }
    vector<vector<int>> dp(money.size() + 1, vector<int>(aim + 1));
    for (int i = 0; i <= money.size(); i++) {
        dp[i][0] = 1;
    }
    for (int Index = money.size() - 1; Index >= 0; Index--) {
        for (int rest = 0; rest <= aim; rest++) {
            int ans = 0;
            for (int cnt = 0; cnt <= counts[Index] && rest - cnt * money[Index] >= 0; cnt++) {
                ans += dp[Index + 1][rest - cnt * money[Index]];
            }
            dp[Index][rest] = ans;
        }
    }
    return dp[0][aim];
}

分析位置依赖

假设来到Index=2位置做决策,并假设此时rest=11,money[2]=3,counts[2]=2,则位置依赖如图:

通过观察可以发现:dp[Index][rest]=dp[Index+1][rest]+dp[Index][rest-money[Index]]-O点(需要对边界进行额外处理),O点的坐标为(Index+1,rest-(counts[Index]+1)*money[Index])。优化代码:

cpp 复制代码
int MethodCount3dp2(vector<int>& arr, int aim) {
    vector<int> counts;
    vector<int> money;
    unordered_map<int, int> hashMap;
    for (int m : arr) {
        hashMap[m]++;
    }
    for (auto [m, c] : hashMap) {
        money.push_back(m);
        counts.push_back(c);
    }
    vector<vector<int>> dp(money.size() + 1, vector<int>(aim + 1));
    for (int i = 0; i <= money.size(); i++) {
        dp[i][0] = 1;
    }
    for (int Index = money.size() - 1; Index >= 0; Index--) {
        for (int rest = 0; rest <= aim; rest++) {
            dp[Index][rest] = dp[Index + 1][rest];
            if (rest - money[Index] >= 0) {
                dp[Index][rest] += dp[Index][rest - money[Index]];
            }
            if (rest - (counts[Index] + 1) * money[Index] > 0) {
                dp[Index][rest] -= dp[Index + 1][rest - (counts[Index] + 1) * money[Index]];
            }
        }
        
    }
    return dp[0][aim];
}

总结

有重复子过程的递归可以尝试将其改写为动态规划,这样会使得解决问题的时间复杂度大大减小,动态规划的状态转移方程其实就是递归的尝试过程,dp表的初始化就是递归的出口,dp表的填表顺序取决于尝试策略,在填表时,若确定dp表的一个位置需要使用for循环,应该尝试分析递归过程和位置依赖,以确定是否可以做进一步的优化。

相关推荐
Funny_AI_LAB21 分钟前
MetaAI最新开源Llama3.2亮点及使用指南
算法·计算机视觉·语言模型·llama·facebook
NuyoahC29 分钟前
算法笔记(十一)——优先级队列(堆)
c++·笔记·算法·优先级队列
jk_10131 分钟前
MATLAB中decomposition函数用法
开发语言·算法·matlab
penguin_bark1 小时前
69. x 的平方根
算法
这可就有点麻烦了1 小时前
强化学习笔记之【TD3算法】
linux·笔记·算法·机器学习
苏宸啊2 小时前
顺序表及其代码实现
数据结构·算法
lin zaixi()2 小时前
贪心思想之——最大子段和问题
数据结构·算法
FindYou.2 小时前
C - Separated Lunch
算法·深度优先
夜雨翦春韭2 小时前
【代码随想录Day30】贪心算法Part04
java·数据结构·算法·leetcode·贪心算法
Kent_J_Truman2 小时前
【平方差 / C】
算法