首页 > 代码库 > 算法分析-动态规划

算法分析-动态规划

 1 var p = [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]; 2  3 var obj = Cut(p, 50); 4 console.log(obj.m) 5 function Cut(p, n) { 6     var m = []; 7     var s = []; 8  9     for (var i = 1; i <= n; i++) {10         m[i] = [];11         s[i] = [];12         for (var j = 1; j <= n; j++) {13             m[i][j] = 0;14             s[i][j] = 0;15         }16     }17 18 19     for (var l = 0; l < n; l++) {20 21         for (var i = 1; i + l <= n; i++) {22 23             var j = i + l;24 25             m[i][j] = p[l] ? p[l] : 0;26 27             for (var k = i; k < j; k++) {28 29                 var temp = m[i][k] + m[k + 1][j];30 31                 if (temp > m[i][j]) {32                     m[i][j] = temp;33                     s[i][j] = k;34                 }35             }36 37         }38     }39     return {40         m: m,41         s: s42     };43 44 }

 

算法分析-动态规划