[Algorithms] Solve Complex Problems in JavaScript with Dynamic Programming
Every dynamic programming algorithm starts with a grid. It entails solving subproblems and builds up to solving the big problem. Let’s break down a problem and solve it in pieces using dynamic programming with JavaScript.
/**
* 给一个浮点数序列,取最大乘积连续子串的值,例如 -2.5,4,0,3,0.5,8,-1,则取出的最大乘积连续子串为3,0.5,8。也就是说,上述数组中,3 0.5 8这3个数的乘积30.58=12是最大的,而且是连续的
* @param {*} a
*/
function MaxProductSubstring (a) {
let maxEnd = a[0]
let maxRes = a[0] for (let i = 1; i < a.length; i++) {
maxEnd = Math.max(maxEnd * a[i], a[i])
maxRes = Math.max(maxRes, maxEnd)
} return maxRes
}
Example two:
const rope = { value: , weight: };
const food = { value: , weight: };
const tent = { value: , weight: };
const iphone = { value: , weight: }; const constraint = [, , , ];
const items = [rope, tent, food, iphone]; /**
* Dynamic progamming
*
* | 1 | 2 | 3 | 4
* rope | 1500 |1500 | 1500 | 1500
* -------------------------------
* tent | 1500 |1500 | 1500 | 3000
* -------------------------------
* food | 1500 |1500 | 2000 | 3500
* -------------------------------
* iphone| 2000 |3500 | 3500 | 4000
* -------------------------------
*
* row(i) = weight > constraint(j) ? row(i-1) : 0
* row(i) = weight = constraint(j) ? ( value > row(i-1) ? value : row(i-1) ) : value
* row(i) = weight < constraint(j) ? value + row[i-1][diff] > row[i-1] ? value + row[i-1][diff] : row[i-1]
* where diff = constraint(j) - weight
*/ function getMaxValue(items, constraint) {
let grid = [...Array(items.length)].map(e => Array(constraint.length)); function helper(items, constraint, grid) {
for (let row in items) {
const { value, weight } = items[row];
for (let col in constraint) {
// take care the first row
if (grid[row - ] === undefined) {
grid[row][col] = weight <= constraint[col] ? value : ;
continue;
} // if weight is larger than constraint, take previous row value
const prevRowSameCol = grid[row - ][col];
if (weight > constraint[col]) {
grid[row][col] = prevRowSameCol;
continue;
} // if weight equals constraint, Max { value , row(i-1)}
if (weight === constraint[col]) {
grid[row][col] = Math.max(value, prevRowSameCol);
continue;
} // if weight samller than constraint, Max { value + row[i-1][diff] , row(i-1)}
if (weight < constraint[col]) {
const diff = constraint[col] - weight - ;
console.log(diff, grid[row - ][diff]);
grid[row][col] = Math.max(
value + grid[row - ][diff],
prevRowSameCol
);
}
}
} return grid;
} return helper(items, constraint, grid);
} const res = getMaxValue(items, constraint);
document.body.append(JSON.stringify(res, null, ));
/**
* [
* [ 1500, 1500, 1500, 1500 ],
* [ 1500, 1500, 1500, 3000 ],
* [ 1500, 1500, 2000, 3500 ],
* [ 2000, 3500, 3500, 4000 ]
* ]
* */
[Algorithms] Solve Complex Problems in JavaScript with Dynamic Programming的更多相关文章
- [Algorithms] Using Dynamic Programming to Solve longest common subsequence problem
Let's say we have two strings: str1 = 'ACDEB' str2 = 'AEBC' We need to find the longest common subse ...
- TED #09# You don't have to be an expert to solve big problems
Tapiwa Chiwewe: You don't have to be an expert to solve big problems Collection noticed a haze hangi ...
- 最优化问题 Optimization Problems & 动态规划 Dynamic Programming
2018-01-12 22:50:06 一.优化问题 优化问题用数学的角度来分析就是去求一个函数或者说方程的极大值或者极小值,通常这种优化问题是有约束条件的,所以也被称为约束优化问题. 约束优化问题( ...
- Speeding Up The Traveling Salesman Using Dynamic Programming
Copied From:https://medium.com/basecs/speeding-up-the-traveling-salesman-using-dynamic-programming-b ...
- Julia is a high-level, high-performance dynamic programming language for technical computing, with syntax that is familiar to users of other technical
http://julialang.org/ julia | source | downloads | docs | blog | community | teaching | publications ...
- Dynamic Programming
We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...
- HDU 4223 Dynamic Programming?(最小连续子序列和的绝对值O(NlogN))
传送门 Description Dynamic Programming, short for DP, is the favorite of iSea. It is a method for solvi ...
- hdu 4223 Dynamic Programming?
Dynamic Programming? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 算法导论学习-Dynamic Programming
转载自:http://blog.csdn.net/speedme/article/details/24231197 1. 什么是动态规划 ------------------------------- ...
随机推荐
- [LeetCode169]Majority Element
Majority Element Total Accepted: 58500 Total Submissions: 163658My Submissions Question Solution Gi ...
- freemarker 前端 判读 遍历 取值
<#if content?length gt 100> ${content[0..100]}... <#else> ${content} </#if> freema ...
- ubuntu xampp
1.下载:首先通过wget下载 xampp linux 1.7.3a版本,地址为http://sourceforge.net/projects/xampp/files/XAMPP%20Linux/ ...
- java:网络编程(InetAddress,InetSocketAddress,URL,TCP(Socket与SeverSocket),TCP与UDP的区别)
InerAddress: /**IP地址:在网络上唯一标示一台计算机 * 端口号:标示计算机上不同的应用程序 * java.net.InetAddress类:此类表示互联网协议 (IP) 地址. * ...
- RQNOJ 34 紧急援救
题目描述 话说2007年8月5日,Mike博士神秘失踪了,最后发现是被外星人绑架了,幸好外星人目前还是在地球上活动,并且知道外星人不了解地球,幸好,Milk博士身上有无线信号发送装置,我们终于确定了他 ...
- Linux用户态定时器用法以及犯错总结【转】
转自:http://blog.csdn.net/csdn_logo/article/details/48525703 版权声明:本文为博主原创文章,欢迎转载,转载请注明出处,多谢合作. 采样的时候要用 ...
- Laravel Model Factory(模型工厂)的用法以及数据本地化
Model Factory的位置 生成数据方法:make是生成数据,create是生成数据并保存到数据库 本地化方法 这样便生成了中文数据 整理自www.laravist.com视频教程
- (4)C#工具箱-菜单和工具栏
1.ContextMenuStrip(右键菜单栏) 把contextMenuStrip控件拖到窗体上,会在窗体下面显示,点击控件在最上行显示菜单栏,可以任意设置.(运行以后不会在界面上显示,它需要预控 ...
- poj3264(Sparse-Table 算法模板)
poj3264 题意 询问区间最大值最小值之差. 分析 dp_max[i][j] 表示以 i 为起点,长度为 \(2^j\) 的区间最大值. 利用递推预处理出区间最大值最小值. code #inclu ...
- JsonConfig处理日期时间
写在前面: 页面发送ajax请求到后台,后台返回对应的json格式数据给前台页面进行数据展示,如果json数据中含有日期时间,就需要对日期进行处理 下面是相关的代码部分 JsonConfig json ...