[Algorithms] Solve Complex Problems in JavaScript with Dynamic Programming

末蓝、 2022-03-27 14:15 274阅读 0赞

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.

  1. /**
  2. * 给一个浮点数序列,取最大乘积连续子串的值,例如 -2.5,4,0,3,0.5,8,-1,则取出的最大乘积连续子串为3,0.5,8。也就是说,上述数组中,3 0.5 8这3个数的乘积30.58=12是最大的,而且是连续的
  3. * @param {*} a
  4. */
  5. function MaxProductSubstring (a) {
  6. let maxEnd = a[0]
  7. let maxRes = a[0]
  8. for (let i = 1; i < a.length; i++) {
  9. maxEnd = Math.max(maxEnd * a[i], a[i])
  10. maxRes = Math.max(maxRes, maxEnd)
  11. }
  12. return maxRes
  13. }

发表评论

表情:
评论列表 (有 0 条评论,274人围观)

还没有评论,来说两句吧...

相关阅读