[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 ...
随机推荐
- 洛谷P2243 电路维修 [最短路]
题目传送门 电路维修 题目背景 Elf 是来自Gliese 星球的少女,由于偶然的原因漂流到了地球上.在她无依无靠的时候,善良的运输队员Mark 和James 收留了她.Elf 很感谢Mark和Jam ...
- java泛型理解。代码更明了。
泛型数据java基础,但真正理解需要悉心品尝.毕竟在工作中用到的是在是太多了. 不要以为new ArrayList<>这就是泛型,这只能属于会使用. 在工作中,相对于现有的项目源码的数据库 ...
- brpc初探
因为最近在看一个内部开源代码,看到了braft.braft又依赖于brpc.于是就看了相关的文档,打算接下来试一把. 这里引用下gejun大佬在知乎上的回答(https://www.zhihu.com ...
- linux下如何配置TCP参数设置详解
设置tcp参数一定要小心谨慎,轻易不要更改线上环境,我贴一下我们线上环境中,sysctl.conf的内容,见文章底部 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tc ...
- 【manacher】模板
考试竟然写错了manacher!太耻辱了!所以赶快又敲了一遍模板!!一定不能错了aaaa #include<iostream> #include<cstdio> #includ ...
- bzoj 2286
第一道"虚树"题目(好吧,我也不知道这是不是虚树,但和虚树的思想肯定是一样的,都是简化树结构) 这一类算法核心思想都是简化树结构,只取我们必须的节点和一些信息,然后在简化后的树结构 ...
- BZOJ 4027: [HEOI2015]兔子与樱花 树上dp
4027: [HEOI2015]兔子与樱花 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline ...
- 如何判断c语言的变量类型
变量三要素: 一个变量有三个基本的要素,变量的名称,变量的类型,变量的值.所以int a = 10; 变量名为a,变量的存储类型为int型,变量的值为10. 变量还有一些属性如作用范围和存储类型. 变 ...
- CodeM资格赛3
题目描述 美团点评上有很多餐馆优惠券,用户可以在美团点评App上购买.每种优惠券有一个唯一的正整数编号.每个人可以拥有多张优惠券,但每种优惠券只能同时拥有至多一张.每种优惠券可以在使用之后继续购买. ...
- mysql_server安装
https://blog.csdn.net/wz1226864411/article/details/76146180