algorithm - 动态规划算法 - 花费 A 美元的方法数
问题描述
我对如何处理这个动态算法问题感到困惑——我想把问题分解成更小的子问题,但我不知道该怎么做
问题:假设您正在购物并计划花费 A 美元。您对 t 件物品感兴趣,每件物品供应无限,因此价值 C1、C2、...、Ct 美元。设计一个动态规划算法来计算花费 A 美元的方式的数量。
任何帮助将不胜感激!
解决方案
I would create an array size + 1 of A.Then iterate on it and inside in iterate the items on increment the value at index you reach by the value where you were.
Let's say A is 4 the array will be all 0. arr = [0 , 0 ,0 ,0 , 0] and item are 1 and 2 dollars.
at step one arr will become [0,1,1,0,0]
step two [0,1,2,1,0]
3 [0,1,2,3,1]
4 [0,1,2,3,4]
And I highly recommend you to check out this question https://www.geeksforgeeks.org/coin-change-dp-7/
推荐阅读
- gitlab - 如何在 Gitlab 上查看文件的所有历史版本?
- ibm-cloud - 在 dropsolve 中带有回调的 docplex
- javascript - 使用地图获取属性是否禁用
- if-statement - 我不想写 60 个 'IF' 命令
- python - TypeError:将形状转换为 TensorShape 时出错:int() 参数必须是字符串或数字,而不是“元组”
- html - 使用 VB.Net 将 Excel 转换为 HTML
- e-commerce - 如何创建 Commerce Repeat Order 链接视图?
- mysql - Laravel 多连接计数
- python - 要锁定并用于运行余额计算的数据框单元格(后续问题)
- go - 与任意切片一起使用的交换实现