博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Algorithms] Solve Complex Problems in JavaScript with Dynamic Programming
阅读量:4584 次
发布时间:2019-06-09

本文共 3018 字,大约阅读时间需要 10 分钟。

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: 1500, weight: 1 };const food = { value: 2000, weight: 3 };const tent = { value: 3000, weight: 4 };const iphone = { value: 2000, weight: 1 };const constraint = [1, 2, 3, 4];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 - 1] === undefined) {          grid[row][col] = weight <= constraint[col] ? value : 0;          continue;        }        // if weight is larger than constraint, take previous row value        const prevRowSameCol = grid[row - 1][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 - 1;          console.log(diff, grid[row - 1][diff]);          grid[row][col] = Math.max(            value + grid[row - 1][diff],            prevRowSameCol          );        }      }    }    return grid;  }  return helper(items, constraint, grid);}const res = getMaxValue(items, constraint);document.body.append(JSON.stringify(res, null, 2));/**  * [  *  [ 1500, 1500, 1500, 1500 ],  *  [ 1500, 1500, 1500, 3000 ],  *  [ 1500, 1500, 2000, 3500 ],  *  [ 2000, 3500, 3500, 4000 ]  * ] * */

 

转载于:https://www.cnblogs.com/Answer1215/p/10177238.html

你可能感兴趣的文章
有限自动机的构造和识别
查看>>
初试机器学习
查看>>
DNS的功能-域名空间、域名注册和域名解析
查看>>
Javascript模块化编程(三):require.js的用法(转)
查看>>
Git使用3(Git操作完整版)
查看>>
sql报错注入:extractvalue、updatexml报错原理
查看>>
C# this.Hide()
查看>>
sqlmap的学习之路-自动化测试SQL注入工具
查看>>
Java 内存管理、JVM 工作原理与 Java 运行时系统
查看>>
矩阵分解(matrix factorization)
查看>>
大型网站的架构设计与演进
查看>>
二值化函数
查看>>
‘3 sigma’rule(68–95–99.7 rule)
查看>>
内存、时间复杂度、CPU/GPU以及运行时间
查看>>
DES加密解决算法
查看>>
【并发编程】延时初始化
查看>>
编程珠玑--左旋字符串
查看>>
【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十四:储存模块
查看>>
模板 - 字符串 - Manacher
查看>>
2017.1.2
查看>>