博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode House Robber
阅读量:5760 次
发布时间:2019-06-18

本文共 1274 字,大约阅读时间需要 4 分钟。

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 for adding this problem and creating all test cases. Also thanks to for adding additional test cases.

 

to see which companies asked this question

 
一道动态规划的题目,想了很久,没想出来。== 最后看陆草纯的代码的。http://www.cnblogs.com/ganganloveu/p/4417485.html
最开始我很简单的想,因为都是非负数嘛,所以就是越多加越好,所以就试了奇数号的加起来求和,偶数号的加起来求和。= = 然而这样行不通的,例如{1,3,1,3,100},{2,1,1,2}
后来看了tag是动态规划的题目,= = 动态规划真是难啊。就是想不明白。。。看了要好好学学动态规划了。。
 
1 class Solution { 2 public: 3     int rob(vector
& nums) { 4 int s=nums.size(); 5 if(s==0) return 0; 6 if(s==1) return nums[0]; 7 vector
maxn(s,0); 8 maxn[0]=nums[0]; 9 maxn[1]=max(nums[0],nums[1]);10 for(int i=2;i

 

 
 

转载于:https://www.cnblogs.com/LUO77/p/4996841.html

你可能感兴趣的文章
Scenario 9-Shared Uplink Set with Active/Active uplink,802.3ad(LACP)-Flex-10
查看>>
UML类图中的六种关系
查看>>
生产者消费者问题理解与Java实现
查看>>
【CentOS 7架构21】,Nginx的安装#180104
查看>>
探寻Interpolator源码,自定义插值器
查看>>
一致性哈希
查看>>
mysql(待整理)
查看>>
看雪论坛502,出现安全宝?
查看>>
springSSM 使用poi导出excel(一)
查看>>
使用TMG配置×××注意事项
查看>>
使用PullToRefresh实现下拉刷新和上拉加载
查看>>
Linux文件系统
查看>>
自适应轮播图
查看>>
mysql
查看>>
SQL Server笔记2
查看>>
安装ansible以及简单使用
查看>>
切换卡TabHost控件的使用
查看>>
管家婆数据库823错误,并闩锁页错误数据恢复成功
查看>>
文华学院新闻稿
查看>>
2012年电信业八大发展趋势
查看>>