LeetCode 198. House Robber
来源:互联网 发布:linux top n 1 编辑:程序博客网 时间:2024/06/03 19:30
题目描述
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.
思路
简单动归。
f(n) = max(num[i] + f(n - 2), f(n - 1))
代码
class Solution: def rob(self, nums): """ :type nums: List[int] :rtype: int """ if len(nums) == 0: return 0 if len(nums) == 1: return nums[0] if len(nums) == 2: return max(nums[0], nums[1]) total_list = [nums[0], max(nums[0], nums[1])] for i, v in enumerate(nums): if i == 0 or i == 1: continue total_list.append(max(total_list[i - 1], total_list[i - 2] + nums[i])) return total_list[len(nums) - 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
- 198. House Robber LeetCode
- [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
- leetcode 198. House Robber
- LeetCode *** 198. House Robber
- leetcode 198. House Robber
- 第八周项目3 对称矩阵压缩存储的实现与应用(1)
- Android 线程通信(Handler + Message + Looper) 1
- 处理IE9下onpropertychange无法触发最简洁的方法
- Android core lifecycle
- 机器学习系列笔记(二)
- LeetCode 198. House Robber
- Python----变量与全局变量
- 利用geoserver发布矢量切片,mapbox进行调用
- MDL:minimum description length principle
- Docker学习系列从零开始之制作docker原始镜像【二】
- android 常用权限和依赖
- python下os.path模块的常见用法
- 官方游戏<恶魔射手>--学习笔记二(英雄脚本)
- vue2.0+vue-cli+webpack+vue-router中路由设定