码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode "k Sum" !!

时间:2015-10-30 02:01:27      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

Great great DP learning experience:
http://www.cnblogs.com/yuzhangcmu/p/4279676.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

LintCode "k Sum" !!

标签:

原文地址:http://www.cnblogs.com/tonix/p/4922254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!