[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. 什么是动态规划 ------------------------------- ...
随机推荐
- 8.2 前端检索的敏感词过滤的Python实现(针对元搜索)
对于前端的搜索内容进行控制,比如敏感词过滤,同样使用socket,这里使用Python语言做一个demo.这里不得不感叹一句,socket真是太神奇了,可以跨语言把功能封装,为前端提供服务. 下面就是 ...
- ie6中margin失效问题
在div的外面添加父级div并设置 padding-bottom: 10px;! <!DOCTYPE html><html><head lang="en&quo ...
- spring的applicationContext.xml如何自动加载
一个web工程自动加载的配置文件只有web.xml,想要加载其他.xml必须在web.xml里面进行配置. 用spring的时候需要一个bean容器来管理所有的bean,所有bean默认是写在appl ...
- 蓝萝卜blu netty3升netty4
老项目是netty3的,本来想直接改到netty5,但是netty5居然是只支持jdk1.7,很奇怪jdk1.6和jdk1.8都不行..为了兼容jdk1.6加上netty4本来和netty5就差别不大 ...
- codevs 1269 匈牙利游戏——次短路(spfa)
欢迎来到匈牙利游戏!布达佩斯(匈牙利首都)的街道形成了一个弯曲的单向网络. 你被强制要求参加一个赛跑作为一个TV秀的一部分节目,比赛中你需要穿越这些街道,从s开始,到t结束. 很自然的,你想要尽快的完 ...
- 汕头市队赛 SRM 09 C 撕书
C 撕书III-3 SRM 09 背景&&描述 琉璃双在撕书. 书总共有n页,每页都可以看作是一个数字. 琉璃读书喜欢来回地读.但他也因此发现了作者的灌水行为:有些连续 ...
- VS2013 Sqlite3 操作指令
extern "C"{ #include "sqlite3.h" }; #pragma comment(lib,"sqlite.lib") ...
- shell脚本查看服务器基本信息
#!/bin/sh #电脑概览 #电脑型号 ComputerModel=`/usr/bin/sudo /usr/sbin/dmidecode | grep -A2 "System Infor ...
- js-监听页面滚动
两种监听页面滚动的方法 一.原生js通过window.onscroll监听 window.onscroll = function() { //为了保证兼容性,这里取两个值,哪个有值取哪一个 //scr ...
- Python的并发并行[0] -> 基本概念
基本概念 / Basic Concept 快速跳转 进程 / Process 线程 / Thread 协程 / Coroutine 全局解释器锁 / Global Interpreter Lock ...