晴明的博客园 GitHub      CodePen      CodeWars     

[js][填坑] 动态规划

#

            //斐波那契数列
            function recurFib(n) {
                if (n < 2) {
                    return n;
                } else {
                    return recurFib(n - 1) + recurFib(n - 2);
                }
            }
            console.log(recurFib(10));
            //动态规划
            function dynFib(n) {
                var val = [];
                for (var i = 0; i <= n; ++i) {
                    val[i] = 0;
                }
                if (n == 1 || n == 2) {
                    return 1;
                } else {
                    val[1] = 1;
                    val[2] = 2;
                    for (var i = 3; i <= n; ++i) {
                        val[i] = val[i - 1] + val[i - 2];
                    }
                    return val[n - 1];
                }
            }
            console.log(dynFib(10));
            //迭代版本的动态规划
            function iterFib(n) {
                var last = 1;
                var nextLast = 1;
                var result = 1;
                for (var i = 2; i < n; ++i) {
                    result = last + nextLast;
                    nextLast = last;
                    last = result;
                }
                return result;
            }
            console.log(iterFib(10));
            //确定两个相同长度字符串中最长公共子串
            function lcs(word1, word2) {
                var max = 0;
                var index = 0;
                var lcsarr = new Array(word1.length + 1);
                for (var i = 0; i <= word1.length + 1; ++i) {
                    lcsarr[i] = new Array(word2.length + 1);
                    for (var j = 0; j <= word2.length + 1; ++j) {
                        lcsarr[i][j] = 0;
                    }
                }
                for (var i = 0; i <= word1.length; ++i) {
                    for (var j = 0; j <= word2.length; ++j) {
                        if (i == 0 || j == 0) {
                            lcsarr[i][j] = 0;
                        } else {
                            if (word1[i - 1] == word2[j - 1]) {
                                lcsarr[i][j] = lcsarr[i - 1][j - 1] + 1;
                            } else {
                                lcsarr[i][j] = 0;
                            }
                        }
                        if (max < lcsarr[i][j]) {
                            max = lcsarr[i][j];
                            index = i;
                        }
                    }
                }
                //console.error(lcsarr);
                //console.error(word1);
                //console.error(word2);
                var str = "";
                if (max == 0) {
                    return "";
                } else {
                    //console.info(index);
                    //console.warn(max);
                    for (var i = index - max; i < index; ++i) {
                        //console.info(i);
                        str += word2[i];
                        //console.log(str);
                    }
                    //console.log(str);
                    return str;
                }
            }
            lcs('dogua', 'xigua');
            lcs('tudou', 'hadou');

#

            //背包问题
            function dKnapsack(capacity, size, value, n) {
                var K = [];
                for (var i = 0; i <= capacity + 1; i++) {
                    K[i] = [];
                }
                for (var i = 0; i <= n; i++) {
                    for (var w = 0; w <= capacity; w++) {
                        if (i == 0 || w == 0) {
                            K[i][w] = 0;
                        } else if (size[i - 1] <= w) {
                            K[i][w] = max(value[i - 1] + K[i - 1][w - size[i - 1]],
                                K[i - 1][w]);
                        } else {
                            K[i][w] = K[i - 1][w];
                        }
                    }
                }
                //console.log(K);
                return K[n][capacity];
            }

            function max(a, b) {
                return (a > b) ? a : b;
            }
            var value = [4, 5, 10, 11, 13];
            var size = [3, 4, 7, 8, 9];
            var capacity = 16;
            var n = 5;
            console.log(dKnapsack(capacity, size, value, n));

#

 

posted @ 2016-03-21 16:13  晴明桑  阅读(164)  评论(0编辑  收藏  举报