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 ...
随机推荐
- Python图片处理库之PIL
这个模块对于Python2.7 的windows64位电脑而言,还真的是不好找啊.这里分享一个下载链接吧,需要的朋友可以下载下来.PIL For Windows64 Python2.7下面分享一下这个 ...
- 安卓自定义View实现钟表
转载请注明出处:http://blog.csdn.net/baiyuliang2013/article/details/45535227 之前实现过html5版的钟表,html5也有一个画板属性Can ...
- java模拟链表
java语言不存在指针,但是我们仍可以用相应的逻辑模拟链表的实现,下面这段代码就是我的一个小伙伴实现的: package com.brucezhang.test; public class ...
- 剑指Offer——二叉树
剑指Offer--二叉树 前言 数据结构通常是编程面试中考察的重点.在参加面试之前,应聘者需要熟练掌握链表.树.栈.队列和哈希表等数据结构,以及它们的操作.本片博文主要讲解二叉树操作的相关知识,主要包 ...
- UNIX网络编程——客户/服务器程序设计示范(三)
TCP预先派生子进程服务器程序,accept无上锁保护 我们的第一个"增强"型服务器程序使用称为预先派生子进程的技术.使用该技术的服务器不像传统意义的并发服务器那样为每个客户现场派 ...
- Swift中实现Observable机制
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51917539 ...
- Andriod Studio科普篇——4.关于编译的常见问题
1.android支持库未安装 编译不过,提示如下: Could not find any version that matches com.android.support:appcompat-v7: ...
- Java进阶(三十一) Web服务调用
Java进阶(三十一) Web服务调用 前言 有朋友问了一个问题:如何调用已知的音乐服务接口,服务文档如下: https://www.evernote.com/shard/s744/sh/c37cd5 ...
- 【一天一道LeetCode】#108. Convert Sorted Array to Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- cocos2d-js(二)cocos2d-js的基本语法与类的简介
基本语法: 1.类的定义 一般类都是集成Scene或者Layer: var myLayer = cc.Layer.extend({类的内容}); 2类内的成员变量与方法: 2.1成员变量的声明: 变量 ...