[Algorithm] Dynamic programming: Find Sets Of Numbers That Add Up To 16
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的更多相关文章
- [Algorithm -- Dynamic Programming] Recursive Staircase Problem
For example there is a staricase N = 3 | ---| |---| | |---| | ---| ...
- [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 ...
- Algorithm: dynamic programming
1. Longest Increasing Subsequence (LIS) problem unsorted array, calculate out the maximum length of ...
- 以计算斐波那契数列为例说说动态规划算法(Dynamic Programming Algorithm Overlapping subproblems Optimal substructure Memoization Tabulation)
动态规划(Dynamic Programming)是求解决策过程(decision process)最优化的数学方法.它的名字和动态没有关系,是Richard Bellman为了唬人而取的. 动态规划 ...
- Speeding Up The Traveling Salesman Using Dynamic Programming
Copied From:https://medium.com/basecs/speeding-up-the-traveling-salesman-using-dynamic-programming-b ...
- Dynamic Programming: From novice to advanced
作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...
- 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 ...
- [Optimization] Dynamic programming
“就是迭代,被众人说得这么玄乎" “之所以归为优化,是因为动态规划本质是一个systemetic bruce force" “因为systemetic,所以比穷举好了许多,就认为是 ...
- About Dynamic Programming
Main Point: Dynamic Programming = Divide + Remember + Guess 1. Divide the key is to find the subprob ...
随机推荐
- Git gitconfig 配置
difftool: [diff] tool = bc4 algorithm = histogram [difftool] prompt = false [difftool "bc4" ...
- 【BZOJ 1019】 1019: [SHOI2008]汉诺塔 (DP?)
1019: [SHOI2008]汉诺塔 Description 汉诺塔由三根柱子(分别用A B C表示)和n个大小互不相同的空心盘子组成.一开始n个盘子都摞在柱子A上,大的在下面,小的在上面,形成了一 ...
- 【插头DP】hdu1964-Pipes
[题目大意] 给出一个网格,经过边要付出代价.求走过每一个格子的欧拉回路的最小代价.[思路] 裸裸的插头DP~然而写了好久orz [错误点] 整个人跟制杖了一样QAQ hash实力写挂…m和n搞反了. ...
- (转载)打破某些大牛比较呵呵的MySQL无file权限读root hash的谣言
如题.比如乌云社区发帖的这位大牛http://zone.wooyun.org/content/12432 看那帖子标题就很喜感有木有,大概意思就是创建了一个没有file权限的账户test,然后不能lo ...
- AIDL原理之 Framewok层实现
AIDLFramework层的架构,如下图: 换而言之,Android就是在传统的C/S架构中加入了一层,实现IPC.图中表明,AIDL类似COM的Proxy/Stub架构.不过是现在android自 ...
- python开发_bisect
现在有如下的需求: ''' 实现这样的一个功能: 对一个班级的学生的成绩做出一些评定,评定规则是: one: [0-60) -- F two: [60-70) -- D three: [70-80) ...
- Internationalization composition diagram
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHJvdWJsZXNob290ZXI=/font/5a6L5L2T/fontsize/400/fill/I0 ...
- NHibernate使用无状态Sessions
NHibernate 3.0 Cookbook第三章,Using stateless sessions的翻译. 当要处理大量的数据,你通常可能会使用更"底层"的API来改善性能,在 ...
- 使用Jacob与Word文件交互
转自:http://www.blogjava.net/lonsy/archive/2009/01/09/250713.html Jacob项目的官方地址: Http://sourceforge.net ...
- android NDK编程:使用posix多线程与mutex相互排斥同步
MainActivity.java 调用原生方法 posixThreads(int threads, int iterations) 启动线程 package com.apress.threads; ...