LeetCode OJ: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.
简单点讲,题目的意思就是,一连串数字,不能去相邻的值,那么怎么样才能取到其中的最大值。DP,表达式为:ret[i] = max(ret[i - 1], ret[i - 2] + nums[i]);
一开始想还有一种可能就是ret[i - 1]可能就是ret[i - 2],但是这里不影响,因为还是ret[i - 2] + num[i]较大,代码如下:
class Solution {
public:
int rob(vector<int>& nums) {
int sz = nums.size();
vector<int> maxRet = nums;
if(sz == ) return ;
if(sz == ) return nums[];
if(sz == ) return max(nums[], nums[]);
int i;
maxRet[] = nums[];
maxRet[] = max(nums[], nums[]);
for(i = ; i < sz; ++i){
maxRet[i] = max(maxRet[i - ] + nums[i], maxRet[i - ]);
}
return maxRet[i - ];
}
};
java版本的代码和上面一样,如下所示:
public class Solution {
public int rob(int[] nums) {
int sz = nums.length;
if(sz == 0)
return 0;
if(sz == 1)
return nums[0];
if(sz == 2)
return Math.max(nums[0], nums[1]);
int [] ret = new int [sz];
ret[0] = nums[0];
ret[1] = Math.max(nums[0], nums[1]);
for(int i = 2; i < sz; ++i){
ret[i] = Math.max(ret[i-2] + nums[i], ret[i-1]);
}
return ret[sz-1];
}
}
LeetCode OJ:House Robber(住宅窃贼)的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- LeetCode OJ 之 Maximal Square (最大的正方形)
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- Leetcode 337. House Robber III
337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...
- LeetCode OJ:Integer to Roman(转换整数到罗马字符)
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
随机推荐
- 022-Spring Boot 构建微服务实战
一.概述 二. 2.1.微服务 将原来一个大的系统,拆分成小系统 每个小系统分别开发,测试,维护 2.2.调用方 服务提供方式:rest(http)[restTemplate,httpclient]. ...
- OpenAI gym的建模思想
一.强化学习问题需要描述那些内容 强化学习中最主要的两类对象是“个体”和“环境”,其次还有一些像“即时奖励”.“收获”.“状态”.“行为”.“价值”.“策略”.“学习”.“控制”等概念.这些概念把个体 ...
- ABAP锁,数据库锁
原文出自 江正军 技术博客,博客链接:www.cnblogs.com/jiangzhengjun ABAP数据锁定 SM12锁查看与维护 通用加锁与解锁函数 ABAP程序锁定 数据库锁 锁的分类和兼容 ...
- 2.4 使用ARDUINO控制MC20进行GPS数据的获取和解析
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
- Binary Search in Java
关于折半查找中的几个注意点. Version 1: public static <T extends Comparable<? super T>> int binSearch( ...
- android各种组件的监听器
<一>Spinner(旋转按钮或下拉列表):设置监听器为:setOnItemSelectedListener 设置动画效果为:setOnTouchListener ...
- iOS JS 和 OC交互 / JS 和 native 相互调用
现在app 上越来越多需求是通过UIWebView 来展示html 或者 html5的内容, js 和 native OC代码交互 就非常常见了. js 调用 native OC代码 第一种机制 ( ...
- 混淆Android JAR包的方法
http://blog.csdn.net/vurtne_ye/article/details/35599491 1)导出jar包 如何在eclipse上Android工程中导出jar包?google, ...
- imx6solo wm8960始终没有声音输出
我尝试各种办法,wm8960始终不能得到声音输出.调试过程如下: 首先,打开电源使能脚: ret=gpio_request(SABRESD_CODEC_PWR_EN,"audio_pwr_e ...
- 主攻ASP.NET.4.5.1 MVC5.0之重生:政府行政网站常用友情链接跳转javascript[干货分享]
<!-----------------------------------> <script language="JavaScript" type="t ...