当前位置:网站首页>【js】-【动态规划】-笔记
【js】-【动态规划】-笔记
2022-07-04 22:59:00 【有趣的学习】
【js】-【动态规划】-笔记
1 如何优雅地找硬币
给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。
示例1:
输入: coins = [1, 2, 5], amount = 11
输出: 3
解释: 11 = 5 + 5 + 1
示例2:
输入: coins = [2], amount = 3
输出: -1
const coinChange = function(coins, amount) {
# 用于保存每个目标总额对应的最小硬币个数
const f = []
f[0] = 0
# 遍历 [1, amount] 这个区间的硬币总额
for(let i=1;i<=amount;i++) {
# 求的是最小值,我们预设为无穷大
f[i] = Infinity
// 循环遍历每个可用硬币的面额
for(let j=0;j<coins.length;j++) {
# 若硬币面额小于目标总额,则问题成立
if(i-coins[j]>=0) {
// 状态转移方程
f[i] = Math.min(f[i],f[i-coins[j]]+1)
}
}
}
// 若目标总额对应的解为无穷大,则意味着没有一个符合条件的硬币总数来更新它,本题无解,返回-1
if(f[amount]===Infinity) {
return -1
}
// 若有解,直接返回解的内容
return f[amount]
};
2 0-1背包问题
有 n 件物品,物品体积用一个名为 w 的数组存起来,物品的价值用一个名为 value 的数组存起来;每件物品的体积用 w[i] 来表示,每件物品的价值用 value[i] 来表示。现在有一个容量为 c 的背包,问你如何选取物品放入背包,才能使得背包内的物品总价值最大?
注意:每种物品都只有1件
# 入参是物品的个数和背包的容量上限,以及物品的重量和价值数组
function knapsack(n, c, w, value) {
// dp是动态规划的状态保存数组
const dp = (new Array(c+1)).fill(0)
# res 用来记录所有组合方案中的最大值
let res = -Infinity
for(let i=1;i<=n;i++) {
for(let v=c;v>=w[i];v--) {
// 写出状态转移方程
dp[v] = Math.max(dp[v], dp[v-w[i]] + value[i])
// 即时更新最大值
if(dp[v] > res) {
res = dp[v]
}
}
}
return res
}
3 最长上升子序列模型
题目描述:给定一个无序的整数数组,找到其中最长上升子序列的长度。
示例:
输入: [10,9,2,5,3,7,101,18]
输出: 4
解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。
var lengthOfLIS = function(nums) {
if(!nums.length) return 0;
# 初始化数组里面每一个索引位的状态值
const dp= new Array(nums.length).fill(1);
let res =1;
for(let i=1;i<nums.length;i++){
for(let j=0;j<i;j++){
# 若遇到了一个比当前元素小的值,则意味着遇到了一个可以延长的上升子序列,故更新当前元素索引位对应的状态
if(nums[j]<nums[i]) dp[i]=Math.max(dp[i],dp[j]+1);
}
res= Math.max(dp[i],res);
}
return res;
};
边栏推荐
- OSEK标准ISO_17356汇总介绍
- MariaDB的Galera集群应用场景--数据库多主多活
- JS card style countdown days
- D3.js+Three. JS data visualization 3D Earth JS special effect
- 【ODX Studio編輯PDX】-0.2-如何對比Compare兩個PDX/ODX文件
- Redis getting started complete tutorial: Geo
- Redis入门完整教程:Redis Shell
- Redis: redis transactions
- PS style JS webpage graffiti board plug-in
- 微信小程序显示样式知识点总结
猜你喜欢

The difference between debug and release

Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click

A complete tutorial for getting started with redis: hyperloglog

Network namespace

Redis入门完整教程:Redis Shell

字体设计符号组合多功能微信小程序源码

MariaDB的Galera集群-双主双活安装设置

浅聊一下中间件

Analysis of the self increasing and self decreasing of C language function parameters

PS style JS webpage graffiti board plug-in
随机推荐
Advanced area a of attack and defense world misc Masters_ good_ idea
Redis入门完整教程:Redis使用场景
一次edu证书站的挖掘
Attack and defense world misc master advanced zone 001 normal_ png
头文件重复定义问题解决“C1014错误“
Sword finger offer 68 - ii The nearest common ancestor of binary tree
Attack and defense world misc advanced area can_ has_ stdio?
Common methods in string class
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
ffmpeg快速剪辑
Redis入门完整教程:Pipeline
Feature scaling normalization
Advanced area of attack and defense world misc 3-11
Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
[odx Studio Edit pdx] - 0.2 - Comment comparer deux fichiers pdx / odx
常用技术指标之一文读懂BOLL布林线指标
[roommate learned to use Bi report data processing in the time of King glory in one game]
MariaDB的Galera集群-双主双活安装设置
Notepad++--编辑的技巧
【剑指offer】1-5题