您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: