LintCode "k Sum" !!
2015-10-30 01:22
387 查看
Great great DP learning experience:
/article/5089407.html
Remember 2 steps of DP: a) setup state transfer equation; b) setup selection strategy.
a) States
From problem statement, we find 3 variables: array size, k and target. So it is 3D DP:
dp[i][j][t]: in previous i elements, we pick j of them, to reach value t - number of results
b) Selection Strategy
Usually for 1D array, selection strategy is very simple: with A[i] - pick it or not. Combined with step a, dp[i][j][t] is result of the 2 choices - pick or not:
dp[i][j][t] = dp[i-1][j-1][t-A[i]](pick it) + dp[i-1][j][t](no pick)
Optimization: dimension i can be eliminated.
Details: To start: all dp[*][0][0] = 1
/article/5089407.html
Remember 2 steps of DP: a) setup state transfer equation; b) setup selection strategy.
a) States
From problem statement, we find 3 variables: array size, k and target. So it is 3D DP:
dp[i][j][t]: in previous i elements, we pick j of them, to reach value t - number of results
b) Selection Strategy
Usually for 1D array, selection strategy is very simple: with A[i] - pick it or not. Combined with step a, dp[i][j][t] is result of the 2 choices - pick or not:
dp[i][j][t] = dp[i-1][j-1][t-A[i]](pick it) + dp[i-1][j][t](no pick)
Optimization: dimension i can be eliminated.
Details: To start: all dp[*][0][0] = 1
相关文章推荐
- uva1462(Fuzzy Google Suggest)
- Forth scrum meeting - 2015/10/29
- Daily Scrumming* 2015.10.29(Day 10)
- HDU1009(贪心)
- 【Alpha】第七次Scrum meeting
- Mac安装Brew
- SVN添加或删除文件操作
- 理解UIImage 拉伸图片 resizableImageWithCapInsets
- maven构建工程引用包源文件加载
- 7、Swift数组的简单使用
- Android笔记(四十五) Android中的数据存储——XML(一)DOM解析器
- 夺命雷公狗jquery---44替换
- 在assets目录下存放本地数据库
- c的网络编程
- WizNote for linux installation
- datediff(),dateadd(),datepart().datename()时间处理函数
- C++中的关键字用法--- explicit
- Android ViewStub
- Gson通过借助TypeToken获取泛型参数的类型的方法(转)
- 配置JDK