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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 最优化问题 Optimization Problems & 动态规划 Dynamic Programming

    2018-01-12 22:50:06 一.优化问题 优化问题用数学的角度来分析就是去求一个函数或者说方程的极大值或者极小值,通常这种优化问题是有约束条件的,所以也被称为约束优化问题. 约束优化问题( ...

  4. Speeding Up The Traveling Salesman Using Dynamic Programming

    Copied From:https://medium.com/basecs/speeding-up-the-traveling-salesman-using-dynamic-programming-b ...

  5. 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 ...

  6. Dynamic Programming

    We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...

  7. HDU 4223 Dynamic Programming?(最小连续子序列和的绝对值O(NlogN))

    传送门 Description Dynamic Programming, short for DP, is the favorite of iSea. It is a method for solvi ...

  8. hdu 4223 Dynamic Programming?

    Dynamic Programming? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  9. 算法导论学习-Dynamic Programming

    转载自:http://blog.csdn.net/speedme/article/details/24231197 1. 什么是动态规划 ------------------------------- ...

随机推荐

  1. RSA加密/解密 Decryption error异常解决

    RSA加密/解密 Decryption error异常解决 import java.io.ByteArrayOutputStream; import java.security.Key; import ...

  2. php 计算函数执行时间的方法及获得微妙的方法

    // 获得微妙方法 function getMillisecond() { list($s1, $s2) = explode(' ', microtime()); return (float)spri ...

  3. Android Handler使用

    1. 介绍 Handler允许向关联线程的消息队列(MessageQueue)发送消息(Message)和可执行对象(Runnable).每个Handler实例都与某个线程(即创建该Handler的线 ...

  4. 【原创】Linux环境下的图形系统和AMD R600显卡编程(7)——AMD显卡的软件中断

    CPU上处理的中断可以分成“硬件中断”和“软件中断”两类,比如网卡产生的中断称为硬件中断,而如果是软件使用诸如"int 0x10"(X86平台上)这样的指令产生中断称为软件中断,硬 ...

  5. Visual Studio Code 好用的 source code editor

    short cut https://code.visualstudio.com/shortcuts/keyboard-shortcuts-linux.pdf go to definition : F1 ...

  6. Kubernetes镜像制作

    #将需要安装的包全部放入一个目录下,然后开始编写Dockerfile#Dockerfile格式FROM #依赖的镜像MAINTAINER #制作者信息WORKDIR #工作目录,打包启动镜像后的所在目 ...

  7. jQuery的动画方法

    /* animate参数: 参数一:要改变的样式属性值,写成字典的形式 参数二:动画持续的时间,单位为毫秒,一般不写单位 参数三:动画曲线,默认为‘swing’,缓冲运动,还可以设置为‘linear’ ...

  8. python的ORM框架SQLAlchemy

    本节内容 ORM介绍 sqlalchemy安装 sqlalchemy基本使用 多外键关联 多对多关系 表结构设计作业  一.ORM介绍 如果写程序用pymysql和程序交互,那是不是要写原生sql语句 ...

  9. 51nod 算法马拉松 34 Problem D 区间求和2 (FFT加速卷积)

    题目链接  51nod 算法马拉松 34  Problem D 在这个题中$2$这个质数比较特殊,所以我们先特判$2$的情况,然后仅考虑大于等于$3$的奇数即可. 首先考虑任意一个点对$(i, j)$ ...

  10. A/B Testing with Practice in Python (Part One)

    I learned A/B testing from a Youtube vedio. The link is https://www.youtube.com/watch?v=Bu7OqjYk0jM. ...