For a given array, we try to find set of pair which sums up as the given target number.

For example, we are given the array and target as:

const data = [, , , ];
const target = ;

We should be able to find 2 pair, which sum up to 16:

{,,}
{,}

We need to create a function to return the number of pair we found, in this case, the answer is: 2

const data = [, , , ];
/**
* Return number of pair found
*/
function DP(data, target = ) {
let memo = {};
return rc(data, target, data.length - , memo);
function rc(data, target, i, memo) {
// Construct the key - value for memo
let key = `${target}-${i}`;
// Store the result
let res = ;
// return the value if already calcualte
if (memo[key]) {
return memo[key];
}
// if the target is zero, then it is empty set, return 1
if (target === ) {
return ;
} else if (target < ) {
// if target < 0, we don't consider the case, return 0
return ;
} else if (i < ) {
// if i <0, means we run out of element, return 0
return ;
}
// if current value is larger than targer, we continue with next value
if (data[i] > target) {
res = rc(data, target, i - , memo);
} else {
/**
* Two cases:
* 1. The current value is not include:
* rc(data, target, i - 1, memo)
* 2. The current value is include: the rest of els should sum up to new target value
* rc(data, target - data[i], i - 1, memo)
*/
// i is not included + i is included
res =
rc(data, target, i - , memo) + rc(data, target - data[i], i - , memo);
}
memo[key] = res;
return res;
}
} console.log(DP(data, )); // 2

Time complexity: O(target * n * 2 + 1) = O(T*N)

[Algorithm] Dynamic programming: Find Sets Of Numbers That Add Up To 16的更多相关文章

  1. [Algorithm -- Dynamic Programming] Recursive Staircase Problem

    For example there is a staricase N = 3 | ---|   |---|    | |---|            | ---|                  ...

  2. [Algorithm -- Dynamic programming] How Many Ways to Decode This Message?

    For example we have 'a' -> 1 'b' -> 2 .. 'z' -> 26 By given "12", we can decode t ...

  3. Algorithm: dynamic programming

    1. Longest Increasing Subsequence (LIS) problem unsorted array, calculate out the maximum length of ...

  4. 以计算斐波那契数列为例说说动态规划算法(Dynamic Programming Algorithm Overlapping subproblems Optimal substructure Memoization Tabulation)

    动态规划(Dynamic Programming)是求解决策过程(decision process)最优化的数学方法.它的名字和动态没有关系,是Richard Bellman为了唬人而取的. 动态规划 ...

  5. Speeding Up The Traveling Salesman Using Dynamic Programming

    Copied From:https://medium.com/basecs/speeding-up-the-traveling-salesman-using-dynamic-programming-b ...

  6. Dynamic Programming: From novice to advanced

    作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...

  7. 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 ...

  8. [Optimization] Dynamic programming

    “就是迭代,被众人说得这么玄乎" “之所以归为优化,是因为动态规划本质是一个systemetic bruce force" “因为systemetic,所以比穷举好了许多,就认为是 ...

  9. About Dynamic Programming

    Main Point: Dynamic Programming = Divide + Remember + Guess 1. Divide the key is to find the subprob ...

随机推荐

  1. 洛谷P1503 鬼子进村 [平衡树,STL]

    题目传送门 鬼子进村 题目背景 小卡正在新家的客厅中看电视.电视里正在播放放了千八百次依旧重播的<亮剑>,剧中李云龙带领的独立团在一个县城遇到了一个鬼子小队,于是独立团与鬼子展开游击战. ...

  2. Hibernate 使用MyEclipse简化开发

    在平时开发中写配置文件比较繁琐,在这里写一下如何使用myEclipse简化开发. 1.打开MyEclipse,创建数据库连接 单机测试连接按钮,如果出现成功建立连接,则连接成功. 然后Finish 2 ...

  3. [BZOJ4338][BJOI2015]糖果(扩展Lucas)

    先求出式子$P_{C_{K+m-1}^{m}}^{n}$,然后对于排列直接$O(n)$求解,对于组合用扩展Lucas求解. 但这题数据并没有保证任何一个模数的质因子的$p^k$在可线性处理的范围内,于 ...

  4. poj 3261 求可重叠的k次最长重复子串

    题意:求可重叠的k次最长重复子串的长度 链接:点我 和poj1743差不多 #include<cstdio> #include<iostream> #include<al ...

  5. QTP 10 破解 之路

    1.下载QTP 10 安装包 2.破解软件(mgn-mqt82.exe)  (阿里云云主机里执行不了,一执行CPU就99%,也没有生成lservrc) 手动创建C:\Program Files (x8 ...

  6. IDA64 Fatal error before kernel init

    http://www.tuicool.com/articles/7FZVZna 第一次看到这个错误还以为是修改文件导致的,但是觉得又不大像,因为在Win7底下是完全正常的.搜索了一下才发现是由于插件导 ...

  7. MP2359 1.2A, 24V, 1.4MHz Step-Down Converter in a TSOT23-6

    The MP2359 is a monolithic step-down switch mode converter with a built-in power MOSFET.It achieves ...

  8. Coreseek:区段查询及增量索引取代实时索引

    1.区段查询 索引系统须要通过主查询来获取所有的文档信息,一种简单的实现是将整个表的数据读入内存,可是这可能导致整个表被锁定并使得其它操作被阻止(比如:在MyISAM格式上的INSERT操作),同一时 ...

  9. iis实现点击文件下载而不是打开文件

    我们平时在搭建网站时,企业网站难免会做一些文档提供给用户下载,有时候我们会遇到提供EXE文件给客户下载时 客户打开文档链接时提示“无法找到该网页”也就是说我们的IIS环境不能下载EXE文件: IIS网 ...

  10. JavaScript中的call和apply应用

    ECMAScript3给Function的原型定义了两个方法,他们是Function.prototype.call 和 Function.prototype.apply. 在实际开发中,特别是在一些函 ...