leetcode 198|213. House Robber 1|2
来源:互联网 发布:蓝牙单片机 编辑:程序博客网 时间:2024/05/16 08:35
198. House Robber
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.
线性动态规划class Solution {public: int rob(vector<int>& nums) { if(nums.size() == 0) return 0; else if(nums.size() == 1) return nums[0]; else if(nums.size() == 2) return max(nums[0], nums[1]); else if(nums.size() == 3) return max(nums[0] + nums[2], nums[1]); nums[2] += nums[0]; for (int i = 3; i < nums.size(); i++) { nums[i] += max(nums[i-2], nums[i-3]); } return max(nums[nums.size()-1], nums[nums.size()-2]); }};
213. House Robber II
Note: This is an extension of House Robber.
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
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.
House Robber I的升级版.因为第一个element 和最后一个element不能同时出现. 则分两次call House Robber I.
case 1: 不包括最后一个element.
case 2: 不包括第一个element.
两者的最大值即为全局最大值
class Solution {public: int rob_helper(vector<int>& nums) { if(nums.size() == 0) return 0; else if(nums.size() == 1) return nums[0]; else if(nums.size() == 2) return max(nums[0], nums[1]); else if(nums.size() == 3) return max(nums[0] + nums[2], nums[1]); nums[2] += nums[0]; for (int i = 3; i < nums.size(); i++) { nums[i] += max(nums[i-2], nums[i-3]); } return max(nums[nums.size()-1], nums[nums.size()-2]); } int rob(vector<int>& nums) { if (nums.size() == 0) return 0; else if (nums.size() == 1) return nums[0]; vector<int> m1 = nums; m1.erase(m1.begin()); vector<int> m2 = nums; m2.pop_back(); return max(rob_helper(m1), rob_helper(m2)); }};
- leetcode 198|213. House Robber 1|2
- LeetCode-198&213.House Robber
- Leetcode 198: House Robber
- leetcode-198 House Robber
- [LeetCode 198]House Robber
- House Robber - LeetCode 198
- leetcode 198 House Robber
- leetCode #198 House Robber
- LeetCode 198-House Robber
- Leetcode[198]-House Robber
- 【Leetcode】House Robber #198
- Leetcode 198 House Robber
- leetcode 198:House Robber
- House Robber (leetcode 198)
- 【LeetCode】198:House Robber
- leetcode[198]:House Robber
- leetcode-198-House Robber
- leetcode 198: House Robber
- Android沉浸式之一步到位(fragment 的坑之解法)
- Android 端天气预报APP的实现(四)使用ViewPager展示天气数据
- board-mx6q_sabresd.c源码阅读笔记
- BZOJ1602 [Usaco2008 Oct]牧场行走
- Spring Batch 学习
- leetcode 198|213. House Robber 1|2
- VLD 的使用
- Python.Numpy学习零碎笔记之矩阵创建
- 软件工程(C编码实践篇)学习总结
- 【蓝桥杯】【买不到的数目】
- 【财富空间】北京航空航天大学王田苗教授:人工智能与机器人前沿科技发展与投资布局
- android studio 格式化代码
- C++学习记录19--运算符重载为什么要使用引用(=)
- 【区块链】宜人智库:审慎乐观—— 2017网贷行业区块链技术应用价值思考