【Leetcode】House Robber II
2016-02-22 04:29
288 查看
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.
---
基于上一次写的house robber,其实这道题会比较有意思。我的思路是这道题的本质其实是,给了一个array,那么第一个element和最后一个element就不能连着,那么我们能不能分成两种array看?就是最后返回的这个抢劫的组合里,要么有第一个元素,要么有最后一个元素,反正不能同时有,那么我们就跟随上一道题的基础上,分别看nums[1:],nums[:-1]。
https://leetcode.com/discuss/36544/simple-ac-solution-in-java-in-o-n-with-explanation
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.
---
基于上一次写的house robber,其实这道题会比较有意思。我的思路是这道题的本质其实是,给了一个array,那么第一个element和最后一个element就不能连着,那么我们能不能分成两种array看?就是最后返回的这个抢劫的组合里,要么有第一个元素,要么有最后一个元素,反正不能同时有,那么我们就跟随上一道题的基础上,分别看nums[1:],nums[:-1]。
public class Solution { public int rob(int[] nums) { if(nums==null || nums.length<1) return 0; if(nums.length<2) return nums[0]; if(nums.length<3) return Math.max(nums[0],nums[1]); int[] arr1; int[] arr2; arr1 = move(nums,0); arr2 = move(nums,nums.length-1); return Math.max(robMany(arr1), robMany(arr2)); } private static int[] move(int[] nums, int index){ ArrayList<Integer> list = new ArrayList<Integer>(); for(int num : nums){ list.add(num); } list.remove(index); int[] ans = new int[nums.length]; for(int i=0;i<list.size();i++) ans[i]=list.get(i); return ans; } public int robMany(int[] nums){ int max = nums[0]; int[] big = new int[nums.length]; big[0] = nums[0]; big[1] = nums[1]; for(int i=2;i<nums.length;i++){ big[i] = nums[i] + max; if(big[i-1] > max) max = big[i-1]; } return big[nums.length-1] > big[nums.length-2]?big[nums.length-1]:big[nums.length-2]; } }分享一个大神的解释:
https://leetcode.com/discuss/36544/simple-ac-solution-in-java-in-o-n-with-explanation
相关文章推荐
- [Immutable.js] Converting Immutable.js Structures to Javascript and other Immutable Types
- Linux发展史
- 怎样的学术导师是好导师(Nature)
- 【Android】11.6 Fragments基本用法示例
- 【NYOJ】[268]荷兰国旗问题
- [Immutable.js] Transforming Immutable Data with Reduce
- 【Android】11.5 创建和管理Fragments
- 按首字母分类并排序显示的列表(二)--给BaseExpandableListAdapter提供数据的HashList
- 【Android】11.4 Fragment及其生命周期
- uva 10277 Boastin' Red Socks
- Java Integer.highestOneBit(i)代码品读
- LeetCode Binary Search Tree Iterator
- 矩阵分解的推荐算法入门-好好看
- 树的重心——树形dp
- matlab练习程序(非负矩阵分解)
- 关于APUE第十六章的客户端程序返回Servname not supported for ai_socktype错误的解决方法
- 【基础技术】Java基础那些事儿系列-cmd和command
- LeetCode Inorder Successor in BST
- 树的最大独立集——树形dp
- CSS-id选择器与class选择器的区别