题解 | #放苹果#
放苹果
https://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
const map = new Map();
void (async function () {
// Write your code here
while ((line = await readline())) {
let input = line.split(' ');
let m = parseInt(input[0]);
let n = parseInt(input[1]);
console.log(getVal(m, n));
}
})();
var getVal = function (m, n) {
if (map.has(`${m}|${n}`)) return map.get(`${m}|${n}`);
// 这里不等于 0 是因为当没有空盘子情况时,(2,2) 等价于 (0,2) 等价于 (0,1) 等于 1
if (m < 0 || n < 0) return 0;
if (m == 1 || n == 1) return 1;
let val;
if (m < n) {
// 必有空盘子
val = getVal(m, n - 1);
} else {
// 分为没有空盘子情况 + 有空盘子情况
val = getVal(m - n, n) + getVal(m, n - 1);
}
map.set(`${m}|${n}`, val);
return val;
};
