为什么我收到类型错误无法在此处读取未定义的属性“0”?另外,我怎样才能完全控制我是通过引用还是通过值传递东西?我无法在任何地方找到如何做到这一点?必须有一些翻转我必须切换以允许这样做。必须记住默认情况下它是通过引用传递还是通过值传递,这真的很烦人。
function lineOfWines(wines) {
let memo = new Array(wines.length)
for (let i = 0; i < memo.length; i++) {
memo[i] = new Array(wines.length).fill(null);
}
return lineOfWinesHelper(0, 0, wines, memo);
}
function lineOfWinesHelper(L, R, wines, memo) {
if (L + R == wines.length) {
return 0;
}
if (memo[L][R] !== null) {
return memo[L][R];
}
sellLeft = (L + R + 1) * wines[L] +
lineOfWinesHelper(L + 1, R, wines, memo);
sellRight = (L + R + 1) * wines[wines.length - 1 - R] +
lineOfWinesHelper(L, R + 1, wines, memo);
memo[L][R] = Math.max(sellLeft, sellRight);
return memo[L][R];
}
function main() {
console.log(lineOfWines([1, 1]));
}
main();