Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
su37josephxia committed Jul 22, 2023
1 parent e03e580 commit cb424e9
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 04-algrithm/01-table/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 问题描述:有n个人,分配到10张桌子上,每张桌子最多坐10个人,每个人至少坐1个人,问有多少种分配方案?
*/
exports.check = function check(remain, pre, max = 10) {
//没人可分配则结束
if (remain < 0) return 0;
if (remain == 0) return 1;
var cnt = 0;
for (var i = pre; i <= max; i++) {//分配到桌子的人数
cnt += check(remain - i, i);
}
return cnt;
}
var memo = {};
exports.checkMem = function checkMem(remain, pre, max = 10) {
if (memo[remain + '-' + pre]) return memo[remain + '-' + pre];
//没人可分配则结束
if (remain < 0) return 0;
if (remain == 0) return 1;
var cnt = 0;
for (var i = pre; i <= max; i++) {//分配到桌子的人数
cnt += checkMem(remain - i, i);
}
return memo[remain + '-' + pre] = cnt;
}

0 comments on commit cb424e9

Please sign in to comment.