We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
原题链接
先明确题意,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警,所以我们得隔着偷。
从 n 个房子中能偷到的最大金额,f(n)。
Math.max(dp[i - 1], dp[i - 2] + nums[i - 1])
dp[0] = 0
dp[1] = nums[0]
const rob = function(nums) { const n = nums.length if (n === 0) return 0 const dp = new Array(n) dp[0] = 0 dp[1] = nums[0] for (let i = 2; i <= n; i++) { dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]) } return dp[n] }
其实,我们实际上只用到了 f(n - 1) 和 f(n - 2) 的结果,并不需要始终持有整个DP数组,每一步只需要前两个最大值,所以两个变量就足够用了。
const rob = function(nums) { const n = nums.length if (n === 0) return 0 let prev = 0 let curr = 0 for (let i = 0; i < n; i++) { let tmp = curr curr = Math.max(curr, prev + nums[i]) prev = tmp } return curr }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接
最优子结构
从 n 个房子中能偷到的最大金额,f(n)。
状态转移方程
Math.max(dp[i - 1], dp[i - 2] + nums[i - 1])
处理边界条件
dp[0] = 0
dp[1] = nums[0]
降维,空间优化
其实,我们实际上只用到了 f(n - 1) 和 f(n - 2) 的结果,并不需要始终持有整个DP数组,每一步只需要前两个最大值,所以两个变量就足够用了。
The text was updated successfully, but these errors were encountered: