[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. 什么是动态规划 ------------------------------- ...
随机推荐
- 笔记:CS231n+assignment2(作业二)(三)
终于来到了最终的大BOSS,卷积神经网络~ 这里我想还是主要关注代码的实现,具体的CNN的知识点想以后在好好写一写,CNN的代码关键就是要加上卷积层和池话层. 一.卷积层 卷积层的前向传播还是比较容易 ...
- SQL 设置自增,和default
mysql数据库为表中已有的主键字段增加自增属性: ALTER TABLE `category ` MODIFY COLUMN `id` int(11) NOT NULL AUTO_INCREMENT ...
- linux缺页异常处理--用户空间【转】
转自:http://blog.csdn.net/vanbreaker/article/details/7870769 版权声明:本文为博主原创文章,未经博主允许不得转载. 用户空间的缺页异常可以分为两 ...
- linux中高端内存和低端内存的概念【转】
转自:http://blog.csdn.net/hdujinhuihui/article/details/8686817 高端内存是Linux中一个重要的概念,初涉Linux时曾经对这个概念非常迷惑. ...
- ubuntu下安装 gSOAP 用于C/C++开发web service服务端与客户端
昨天在ubuntu下进行安装gSOAP,费了很多时间,没成功,今天又来找了大量教程资料,终于一次成功,这里写下自己的安装步骤和方法,供大家参考. 首先下载gsoap,我下载的是gsoap-2.8.1. ...
- 安装戴尔OMSA
设置变量versionum=`cat /etc/redhat-release | awk '{print $3}' | awk -F '.' '{print $1}'`versionname=`cat ...
- C# split字符串
string strSourse = "ab|||cdef"; string[] arr = strSource.Split(new string[]{"|||" ...
- HTML,DIV+CSS,js,JQ,UI-WEB前端设计经验
目前比较全的CSS重设(reset)方法总结 在当今网页设计/开发实践中,使用CSS来为语义化的(X)HTML标记添加样式风格是重要的关键.在设计师们的梦想中都存在着这样的一个完美世界:所有的浏览 ...
- NetStream论文
https://max.book118.com/html/2016/0102/32573670.shtm http://www.docin.com/p-1568348795.html
- 计蒜客 28319.Interesting Integers-类似斐波那契数列-递推思维题 (Benelux Algorithm Programming Contest 2014 Final ACM-ICPC Asia Training League 暑假第一阶段第二场 I)
I. Interesting Integers 传送门 应该是叫思维题吧,反正敲一下脑壳才知道自己哪里写错了.要敢于暴力. 这个题的题意就是给你一个数,让你逆推出递推的最开始的两个数(假设一开始的两个 ...