1 Star 0 Fork 2

原罪/javascript-algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
integerPartition.js 2.59 KB
一键复制 编辑 原始数据 按行查看 历史
Oleksii Trekhleb 提交于 2018-06-22 19:50 +08:00 . Update README for integer partition.
/**
* @param {number} number
* @return {number}
*/
export default function integerPartition(number) {
// Create partition matrix for solving this task using Dynamic Programming.
const partitionMatrix = Array(number + 1).fill(null).map(() => {
return Array(number + 1).fill(null);
});
// Fill partition matrix with initial values.
// Let's fill the first row that represents how many ways we would have
// to combine the numbers 1, 2, 3, ..., n with number 0. We would have zero
// ways obviously since with zero number we may form only zero.
for (let numberIndex = 1; numberIndex <= number; numberIndex += 1) {
partitionMatrix[0][numberIndex] = 0;
}
// Let's fill the first column. It represents the number of ways we can form
// number zero out of numbers 0, 0 and 1, 0 and 1 and 2, 0 and 1 and 2 and 3, ...
// Obviously there is only one way we could form number 0
// and it is with number 0 itself.
for (let summandIndex = 0; summandIndex <= number; summandIndex += 1) {
partitionMatrix[summandIndex][0] = 1;
}
// Now let's go through other possible options of how we could form number m out of
// summands 0, 1, ..., m using Dynamic Programming approach.
for (let summandIndex = 1; summandIndex <= number; summandIndex += 1) {
for (let numberIndex = 1; numberIndex <= number; numberIndex += 1) {
if (summandIndex > numberIndex) {
// If summand number is bigger then current number itself then just it won't add
// any new ways of forming the number. Thus we may just copy the number from row above.
partitionMatrix[summandIndex][numberIndex] = partitionMatrix[summandIndex - 1][numberIndex];
} else {
/*
* The number of combinations would equal to number of combinations of forming the same
* number but WITHOUT current summand number PLUS number of combinations of forming the
* <current number - current summand> number but WITH current summand.
*
* Example:
* Number of ways to form 5 using summands {0, 1, 2} would equal the SUM of:
* - number of ways to form 5 using summands {0, 1} (we've excluded summand 2)
* - number of ways to form 3 (because 5 - 2 = 3) using summands {0, 1, 2}
* (we've included summand 2)
*/
const combosWithoutSummand = partitionMatrix[summandIndex - 1][numberIndex];
const combosWithSummand = partitionMatrix[summandIndex][numberIndex - summandIndex];
partitionMatrix[summandIndex][numberIndex] = combosWithoutSummand + combosWithSummand;
}
}
}
return partitionMatrix[number][number];
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wzhgitee/javascript-algorithms.git
git@gitee.com:wzhgitee/javascript-algorithms.git
wzhgitee
javascript-algorithms
javascript-algorithms
master

搜索帮助