House Robber
2015-07-09 16:07
761 查看
1 题目描述
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 itwill 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.
题目出处:https://leetcode.com/problems/house-robber/
2 解题思路
本题属于动态规划的题,主要是找到动态规划的方程。方法可以参考http://www.cnblogs.com/ganganloveu/p/4417485.html3 源代码
package com.larry.easy; public class HouseRobber { public int rob(int[] nums) { //max表示第i个房的最大收益 int len = nums.length; int max[] = new int[len]; if(len == 0) return 0; else if(len == 1) return nums[0]; else{ max[0] = nums[0]; if(nums[0] >= nums[1]) max[1] = nums[0]; else max[1] = nums[1]; for(int i = 2; i < len; i++) max[i] = Math.max(max[i-2] + nums[i], max[i-1]); } return max[len-1]; } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统