Leetcode House Robber
2016-06-15 11:59
316 查看
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.
Difficulty: Easy
Solution: DP
public class Solution {
public int rob(int[] nums) {
int len = nums.length;
if(len == 0) return 0;
int ans1 = nums[0];
int ans2 = 0;
for(int i = 1; i < len; i++){
int temp1 = ans1;
int temp2 = ans2;
ans1 = Math.max(temp1,temp2 + nums[i]);
ans2 = Math.max(temp1, temp2);
}
return Math.max(ans1, ans2);
}
}
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.
Difficulty: Easy
Solution: DP
public class Solution {
public int rob(int[] nums) {
int len = nums.length;
if(len == 0) return 0;
int ans1 = nums[0];
int ans2 = 0;
for(int i = 1; i < len; i++){
int temp1 = ans1;
int temp2 = ans2;
ans1 = Math.max(temp1,temp2 + nums[i]);
ans2 = Math.max(temp1, temp2);
}
return Math.max(ans1, ans2);
}
}
相关文章推荐
- haproxy安装配置
- 关于logistic regression小议
- 关于logistic regression小议
- Android通知栏实现APP下载更新(DownloadManager和自定义Notification2种方式实现)
- awk linux
- Java多线程编程中synchronized关键字的基础用法讲解
- XML知识索引
- cgi php-cgi fastcgi php-fpm Spawn-FCGI关系
- 学习笔记,画布说
- Java最基础的面试题有哪些
- EAIntroView–高度可定制的iOS应用欢迎页通用解决方案
- nginx 禁止某个IP访问
- Android targetSdkVersion 原理
- 安卓学习之--如何关闭所有的activity
- hello world
- 连续分配方式 --(单一连续/固定分区)
- Leetcode Word Ladder
- PPT动画教程:配色方案
- QuickSort(递归)
- Python Web 2 —— 类和函数的声明和调用