From cb424e98d80230c2d9534015da18820ad9877b8e Mon Sep 17 00:00:00 2001 From: josephxia <15906475@qq.com> Date: Sat, 22 Jul 2023 12:29:13 +0800 Subject: [PATCH] update --- 04-algrithm/01-table/index.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 04-algrithm/01-table/index.js diff --git a/04-algrithm/01-table/index.js b/04-algrithm/01-table/index.js new file mode 100644 index 0000000..c880a75 --- /dev/null +++ b/04-algrithm/01-table/index.js @@ -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; +}