House Robber II:打家劫舍 取非相邻元素求和最大,且认为第一个元素与最后一个元素相邻
来源:互联网 发布:java 启动参数 编辑:程序博客网 时间:2024/06/05 01:00
Note: This is an extension of House Robber.
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
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.
打家劫舍 取非相邻元素求和最大,且认为第一个元素与最后一个元素相邻
举例:1 1 1 输出1
1 2 3 1 输出4
0 输出0
1 3 输出3
思路:标准的动态规划,和普通的House Robber.类似,因为第一个元素与最后一个元素不能同时取,所以分别对0-(n-2)与1-(n-1)各位元素取符合题意的最值,再取最大者。
代码写的很啰嗦,可以看Leetcode上的简洁版┗|`O′|┛ 嗷~~
class Solution { int result = 0; public int get(int[] nums ){ if(nums.length==0) return 0; if(nums.length==1) return nums[0]; if(nums.length==2) return Math.max(nums[0],nums[1]); int d[] = new int[nums.length+1]; d[0] = 0; d[1] = nums[0]; d[2] = Math.max(nums[0],nums[1]); for(int i = 3;i<=nums.length;i++){ d[i] = Math.max(d[i-1],d[i-2]+nums[i-1]); } return d[nums.length]; } public int rob(int[] nums) { if(nums.length==0) return 0; if(nums.length==1) return nums[0]; if(nums.length==2) return Math.max(nums[0],nums[1]); int[] a1 = new int[nums.length]; int[] a2 = new int[nums.length]; for(int i = 0;i<nums.length-1;i++){ a1[i] = nums[i]; a2[i] = nums[i+1]; } return Math.max(get(a1),get(a2)); }}简化版本:
private int rob(int[] num, int lo, int hi) { int include = 0, exclude = 0;//对于数组中的每一个元素,都包含两种选法:选取当前元素include,不选取当前元素exclude for (int j = lo; j <= hi; j++) { int i = include, e = exclude;//include表示选取当前元素的选法,exclude表示不选取当前元素的选法 include = e + num[j];//如果选取当前元素,那么就得选择没有选取前一元素的选法,因为相邻元素不能同时取 exclude = Math.max(e, i);//对于不选取当前元素的选法,应该是之前一步中总和最大的两种选法 } return Math.max(include, exclude);/选择两种选法中最大的选法}public int rob(int[] nums) {//由于第一个元素与最后一个元素不能同时选取,所以把他们分开成两组,在分别计算最优值,取最大者 if (nums.length == 1) return nums[0]; return Math.max(rob(nums, 0, nums.length - 2), rob(nums, 1, nums.length - 1));}
- House Robber II:打家劫舍 取非相邻元素求和最大,且认为第一个元素与最后一个元素相邻
- House Robber III:打家劫舍 在二叉树结构中取非相邻元素求和取最大
- House Robber:在不取相邻元素的情况下对数组求和
- [LeetCode] House Robber II 求循环数组中元素两两不相邻的子序列最大和
- LeetCode (20) house robber (数组不相邻元素最大值)
- [LeetCode] House Robber 求数组中元素两两不相邻的子序列最大和
- 数组最大相邻元素之和
- House Robber I - 由数组中不相邻元素组成的子数组,使其和最大
- 输入一个数组,最大的元素与第一个元素交换,最小的元素与最后一个元素交换
- 读一组整数到vector 对象,计算并输出每对相邻元素的和。如果读入元素个数 为奇数,则提示用户最后一个元素没有求和,并输出其值。然后修改程序:头 尾元素两两配对(第一个和最后一个,第二个和倒数第二个
- 给定一个非负数组,求不相邻元素的最大和。
- 求一个数组中最大的相邻元素之和
- 单链表交换相邻元素
- adjacent_find 查找相邻元素
- 【读书笔记】输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
- 用户输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
- 输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。。
- java 输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
- 基于html5 Canvas图表库 : ECharts
- C++解析字符串,调用函数
- 集群时间同步
- 在ROS上发布点云 PointClouds
- $$x^y+y^x>1$$
- House Robber II:打家劫舍 取非相邻元素求和最大,且认为第一个元素与最后一个元素相邻
- opencv学习笔记第四章 计算图像的直方图
- jquery对象的基本方法和属性--学习笔记
- leetcode--24. Swap Nodes in Pairs
- 抽象和接口
- Servlet细节——配置文件
- grails的成功配置
- 赠书 | 如何用 JavaScript 控制 Arduino?
- Linux数据恢复的研究