C++ 最大非相邻元素累加和算法实现
int maxNonAdjacentSum(vector
for (int i = 2; i < n; i++) {
dp[i] = max(dp[i-1], dp[i-2] + nums[i]); // 状态转移方程
}
return dp[n-1];
}
原文地址: https://www.cveoy.top/t/topic/qoLh 著作权归作者所有。请勿转载和采集!
安全问答是一个知识全球问答,包含丰富的问答知识
int maxNonAdjacentSum(vector
for (int i = 2; i < n; i++) {
dp[i] = max(dp[i-1], dp[i-2] + nums[i]); // 状态转移方程
}
return dp[n-1];
}
原文地址: https://www.cveoy.top/t/topic/qoLh 著作权归作者所有。请勿转载和采集!