LeetCode之旅(22)-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.
思路:
- 本质是求一个非负整数数组a【n】的非相邻元素的最大和。
- 这一类问题要用动态规划的思路,把数组的长度,作为每一步。设置数组count【n】作为数组的每一步的最大的和。存在推倒关系:count【n】 = max(count【n-1】,(count【n-2】+a【n】));
-
代码:
public class Solution {
public int rob(int[] nums) {
int[] count = null;
int n = nums.length;
if(n == 0){
return 0;
}
if(n == 1){
return nums[0];
}
if(n > 1){
count = new int[n];
}
count[0] = nums[0];
if(nums[1] > nums[0]){
count[1] = nums[1];
}else{
count[1] = nums[0];
}
for(int i = 2;i < n;i++){
if((count[i-2]+nums[i]) > count[i-1]){
count[i] = count[i-2]+nums[i];
}else{
count[i] = count[i-1];
}
}
return count[n-1];
}
}
LeetCode之旅(22)-House Robber的更多相关文章
- leetcode之旅(11)-Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- LeetCode之旅(13)-Valid Anagram
题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
- LeetCode之“动态规划”:House Robber && House Robber II
House Robber题目链接 House Robber II题目链接 1. House Robber 题目要求: You are a professional robber planning to ...
- LeetCode之旅(18)-Happy Number
题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...
- <LeetCode OJ> 337. House Robber III
Total Accepted: 1341 Total Submissions: 3744 Difficulty: Medium The thief has found himself a new pl ...
- [算法学习]开始leetcode之旅
在此记录一下用javascript刷leetcode的过程,每天都要坚持! 1.Two Sum Given an array of integers, find two numbers such th ...
- SAM4E单片机之旅——22、GMAC和PHY的介绍与初始化
网络通信的作用不用多说,而这次进行的工作即是对以太网通信过程中,需要用到的硬件部分进行初始化,也介绍了发送和接收数据的方法. 由于较为复杂,所以使用了ASF框架.但是也会对用到的库函数的实现做一个介绍 ...
- 【leetcode❤python】198. House Robber
class Solution(object): def rob(self, nums): """ :type nums: List[in ...
- Leetcode题解(22)
66. Plus One 题目 这题很简单,直接代码: class Solution { public: vector<int> plusOne(vector<int> &am ...
随机推荐
- 剑指Offer——CVTE校招笔试题+知识点总结(Java岗)
剑指Offer(Java岗)--CVTE校招笔试题+知识点总结 2016.9.3 19:00参加CVTE笔试,笔试内容如下: 需要掌握的知识:Linux基本命令.网络协议.数据库.数据结构. 选择题 ...
- 自己动手实现一个Android Studio插件
在使用Android Studio开发的时候,大部分人都会使用一些插件来提高开发效率,例如我们所熟知的butternife,selector,,GsonFormat等,这些分别从不同的原理来帮助我们提 ...
- 返回present的根
//返回四大tab页面 + (void)gobackToTabarController { UINavigationController* selectedTabNavController = (UI ...
- (一一七)基本文件操作 -SDWebImage清除缓存 -文件夹的大小计算
在iOS的App沙盒中,Documents和Library/Preferences都会被备份到iCloud,因此只适合放置一些记录文件,例如plist.数据库文件.缓存一般放置到Library/Cac ...
- NoSQL数据库之Redis数据库:Redis的介绍与安装部署
NoSQL(NoSQL = Not Only SQL),它指的是非关系型的数据库.随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的w ...
- request.setAttribute()怎么用的?
request.setAttribute()怎么用的? JSP1代码 String [] test=new String[2]; test[0]="1"; test[1]=&quo ...
- MTK机器原始OTA更新方法
在源码中编译完成后会生成各类.img的文件,这时候make otapackage生成ota包 一般ota包在源码工程的out/target/...目录下 一.通过线刷模式 将生成OTA包拷贝到Wind ...
- 用 Freemarker 生成 word 文档
阅读目录 添加图片 自定义载入模板 1. 用word写一个需要导出的word模板,然后存为xml格式. 2. 将xml中需要动态修改内容的地方,换成freemarker的 ...
- jdbc连接sql数据库
JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...
- 基于FT5x06嵌入式Linux电容触摸屏驱动
**************************************************************************************************** ...