题解 | #数的划分#
数的划分
http://www.nowcoder.com/practice/24c2045f2cce40a5bf410a369a001da8
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int 被划分的数
* @param k int 化成k份
* @return int
*/
public int divideNumber(int n, int k) {
// write code here
if (k > n) {
return 0;
}
if (n == k || k == 1 || n == k + 1) {
return 1;
}
int mod = 1000000007;
int[][] dp = new int[n + 1][k + 1];
dp[0][0] = 1;
// 当前的数为 n,最多划分成 n 份(即 k <= n)
for (int currentNumber = 1; currentNumber <= n; currentNumber++) {
for (int currentSplit = 1; currentSplit <= k; currentSplit++) {
if (currentSplit <= currentNumber) {
dp[currentNumber][currentSplit] = (dp[currentNumber - 1][currentSplit - 1] + dp[currentNumber - currentSplit][currentSplit]) % mod;
}
}
}
return dp[n][k];
}
}