[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 ...
随机推荐
- Python类总结-封装(Property, setter, deleter)
Property #property #内置装饰器函数,只在面向对象中使用 from math import pi class Circle: def __init__(self,r ): self. ...
- 恢复mysql数据库误删数据
前言 某一天,天朗气清:突然传来消息:数据库被删库了!这简直不亚于8级大地震呀:一找原因,服务器宕机造成了数据库数据丢失.于是,通过日志恢复数据的救援开始了. 正文 在数据库开启binlog功能 找到 ...
- Tweet信息搜集工具tinfoleak
Tweet信息搜集工具tinfoleak 推特是国外用户常用的社交网站.通过分析用户发布的推文以及社交活动,可以获取大量的个人信息.Kali Linux新增一款Tweet信息搜索工具tinfole ...
- RxSwift 系列(九)
前言 看完本系列前面几篇之后,估计大家也还是有点懵逼,本系列前八篇也都是参考RxSwift官方文档和一些概念做的解读.上几篇文章概念性的东西有点多,一时也是很难全部记住,大家脑子里面知道有这么个概念就 ...
- [BZOJ4247]挂饰(DP)
当最终挂饰集合确定了,一定是先挂挂钩多的在挂挂钩少的. 于是按挂钩从大到小排序,然后就是简单的01背包. #include<cstdio> #include<algorithm> ...
- POJ 3162 Walking Race 树形dp 优先队列
http://poj.org/problem?id=3162 题意 : 一棵n个节点的树.wc爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远的那个节点(产生了n个距离),现在要 ...
- bzoj 2770 堆的中序遍历性质
我们知道二叉搜索树的中序遍历是一个已经排好序的序列,知道序列我们无法确定树的形态(因为有多种). 但是,Treap如果告诉我们它的关键字以及权值,那么就可以唯一确定树的形态(Treap的O(logn) ...
- phpRedis函数使用总结
/*1.Connection*/ $redis = new Redis(); $redis->connect('127.0.0.1',6379,1);//短链接,本地host,端口为6379,超 ...
- SPOJ 10628. SPOJ COT Count on a tree 可持久化线段树
这题是裸的主席树,每个节点建一棵主席树,再加个lca就可以了. 历尽艰辛,终于A掉了这一题,这般艰辛也显示出了打代码的不熟练. 错误:1.lca倍增的时候i和j写反了,RE了5次,实在要吸取教训 2. ...
- PAT甲级1098. Insertion or Heap Sort
PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...