leetcode:198 House Robber-每日编程第二十二题
2015-12-13 10:54
656 查看
House Robber
Total Accepted: 44999
Total Submissions: 140629
Difficulty: Easy
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.
思路:arr[i]表示以第i个被偷为前提,前i+1个屋子最多可以偷多少钱。
arr[i]=max(arr[i-2],arr[i-3])+num[i];
Total Accepted: 44999
Total Submissions: 140629
Difficulty: Easy
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.
思路:arr[i]表示以第i个被偷为前提,前i+1个屋子最多可以偷多少钱。
arr[i]=max(arr[i-2],arr[i-3])+num[i];
class Solution { public: int rob(vector<int>& nums) { int size = nums.size(); if(size==0){ return 0; }else if(size==1){ return nums[0]; }else if(size==2){ return (nums[0]>nums[1]?nums[0]:nums[1]); }else if(size==3){ return ((nums[1]>nums[0]+nums[2])?nums[1]:(nums[0]+nums[2])); } int* arr = new int[size]; arr[0]=nums[0]; arr[1]=nums[1]; arr[2]=nums[0]+nums[2]; for(int i=3;i<size;i++){ arr[i]=nums[i]+(arr[i-2]>arr[i-3]?arr[i-2]:arr[i-3]); } return (arr[size-1]>arr[size-2]?arr[size-1]:arr[size-2]); } };
相关文章推荐
- Java动态代理的实现
- C# DateTime 日期加1天 减一天 加一月 减一月 等方法
- zip php压缩类
- 文件共享服务 FTP,NFS 和 Samba
- c++函数
- c++中设计可以统计自身对象的类(1)
- windows系统下Python环境的搭建
- 课时9第三章:面线对象编程-OOP-特征2
- php解析html类库simple_html_dom
- python基础教程共60课-第37课字典
- php编译原理
- 谁分配、谁释放的原则需要goto
- STL源代码分析——STL算法remove删除算法
- 课时8第三章:面向对象编程-OOP-特征1
- 12.12课堂学习----异常代码案例
- [javase学习笔记]-4.1 函数的定义
- php发送邮件类
- php 去除bom 类
- SpringMVC注释启用
- python基础教程共60课-第36课异常处理