-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e03e580
commit cb424e9
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |