[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. 什么是动态规划 ------------------------------- ...
随机推荐
- [SCOI2005]互不侵犯 (状压$dp$)
题目链接 Solution 状压 \(dp\) . \(f[i][j][k]\) 代表前 \(i\) 列中 , 已经安置 \(j\) 位国王,且最后一位状态为 \(k\) . 然后就可以很轻松的转移了 ...
- 优化web前端性能的几个方法
1 减少http请求, a. 合并脚本跟样式文件,如可以把多个 CSS 文件合成一个,把多个 JS 文件合成一个. b. CSS Sprites 利用 CSS background 相关元素进行背景图 ...
- [03] html 中引入与使用css
1. 使用style属性 <a style="color: red;"> hello ,there use style attribute</a> 2. l ...
- git隐藏文件复制
从网上down的开源项目,如何添加到自己的github上呢? 问题:直接复制老项目到自己的目录,隐藏的.git文件不会被复制过去,就算是执行cp命令,也不会复制!导致项目运行会出错!! 解决: ...
- 开源免费的C/C++网络库(c/c++ sockets library)(转)
原文转自 http://blog.csdn.net/weiwangchao_/article/details/8730199 (1)ACE 庞大.复杂,适合大型项目.开源.免费,不依赖第三方库,支持跨 ...
- input框监控输入内容
$(".input").bind("input porpertychange",function(){ console.log($(".input&q ...
- Android (Notification)消息推送机制
从网上查询资料学习Android消息推送机制,效果图如下: 1.首先是布局文件代码 activity_main.xml <?xml version="1.0" encodin ...
- Javascript"怪异"现象
下面给大家看个例子,这个毫无疑问打印出10 var a = 10; function test() { console.log(a); } test(); 下面我改动一下 var a = 10; fu ...
- 请教一下16aspx上的源代码要如何在自己的服务器上运行
很正常呀,,我下载的也有运行不成功的你要去他们16aspx论坛发帖子问这里很少有人回答你这样的问题
- axure8.1.0.3377授权码
被授权人:zdfans.com 授权密钥:gP5uuK2gH+iIVO3YFZwoKyxAdHpXRGNnZWN8Obntqv7++FF3pAz7dTu8B61ySxli