LeetCode 198. House Robber - 贪心算法
2017-10-30 17:58
513 查看
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.
题意:
一个数组里面存有每个房子的钱,如果取两个相邻的房子会报警。问怎么取钱最多。
思路:
贪心取,取当前所在房子的钱时,有两种考虑。
取,但是它左边的那个不能取
不取,取它左边的那个
代码:
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.
题意:
一个数组里面存有每个房子的钱,如果取两个相邻的房子会报警。问怎么取钱最多。
思路:
贪心取,取当前所在房子的钱时,有两种考虑。
取,但是它左边的那个不能取
不取,取它左边的那个
代码:
class Solution(object): def rob(self, nums): """ :type nums: List[int] :rtype: int """ dp = [0] + nums for i in range(2, len(dp)): dp[i] = max(dp[i-1], dp[i-2] + dp[i]) return dp[-1]
相关文章推荐
- leetcode 198. House Robber
- LeetCode 198. House Robber
- 【LeetCode】198. House Robber
- leetcode 198. House Robber
- [Leetcode] 198. House Robber 解题报告
- LeetCode_198. House Robber_动态规划
- Leetcode-198. House Robber,213. House Robber II
- leetcode 198. House Robber 入室抢劫 + DP动态规划求解
- 【LeetCode】 198. House Robber
- LeetCode 198. House Robber
- leetcode 198. House Robber
- Leetcode:198. House Robber(JAVA)
- Leetcode-198. House Robber
- 【leetcode】198. House Robber
- LeetCode-198. House Robber (JAVA)寻找数组不相邻组合最大值DP
- [LeetCode]problem 198. House Robber
- 【Leetcode】198. House Robber - - 【动态规划】
- LeetCode 198. House Robber
- 198. House Robber,又发现一道数学归纳法在leetcode中的应用题
- Leetcode——198. House Robber