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. 自定义寄存器出现error C142: 'SFR': invalid base address

    今天打算自定义一个.H文件来写写代码.自定义寄存器的时候发现出现这样的问题7816.H(5): error C142: 'SFR': invalid base address. 下面是我自定义的寄存器 ...

  2. redis优化方案

    流水线(pipelined) 批量提交redis命令,减少通信次数 事务 mulit,事务的开始 exec,执行事务快内的命令 discard,放弃事务快内的命令 watch,监视key,如果key改 ...

  3. 【2-SAT】The Ministers’ Major Mess UVALive – 4452

    题目链接:https://cn.vjudge.net/contest/209474#problem/C 题目大意: 一共有m个提案,n个政客,每个政客都会对一些提案(最多四个)提出自己的意见——通过或 ...

  4. 后台开发常用mysql语句_v1.0

    目录 一.基本信息查看 1. 表描述 二.表操作 1. 查看建表语句 2.查看表 3. 创建表 4. 更新表 5. 删除表 6. 重命名表 三.索引操作 1. 查看索引 2. 创建索引 3. 修改索引 ...

  5. shell kill session

    ps -ef | grep java kill -9 pid

  6. Unity Shader 之 透明效果

    透明效果 透明效果一般有两种实现方法: 第一种,使用透明度测试(Alpha Test) 第二种,使用透明度混合(Alpha Blending) 透明度测试和透明度混合机制: 透明度测试(Alpha T ...

  7. BZOJ3669 NOI2014魔法森林

    按a从小到大排序,然后按b建图. 每次只需要找1~n中最大的b加当前的a计算答案即可. 这里还有一个小操作就是化边为点,把一条边的边权看做一个点的点权然后多连两条边. By:大奕哥 #include& ...

  8. 【SDOI2017】树点染色【线段树+LCT】

    本来只是想练练LCT,没想到是个线段树 对于操作1:诶新的颜色?这不是access吗? 也就是说,我们用一棵splay来表示一种颜色 操作2直接在LCT上乱搞-- 不对啊,操作3要查子树 诶好像是静态 ...

  9. [AtCoder-ARC073F]Many Moves

    题目大意: 有一排n个格子和2枚硬币. 现在有q次任务,每一次要你把其中一枚硬币移到x的位置上,移动1格的代价是1. 两枚硬币不能同时移动,任务必须按次序完成. 现在告诉你两枚硬币初始状态所在的位置a ...

  10. java - 内存泄漏

    内存泄漏问题产生原因 长生命周期的对象持有短生命周期对象的引用就很可能发生内存泄露,尽管短生命周期对象已经不再需要,但是因为长生命周期对象持有它的引用而导致不能被回收,这就是java中内存泄露的发生场 ...