本文主要是介绍house robber follow up,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
输出抢劫的house
public static void main(String[] args) {// rob(new int[]{4,1,2,7,5,3,1});// rob(new int[]{1,1,3,6,7,10,7,1,8,5,9,1,4,4,3});// rob(new// int[]{183,219,57,193,94,233,202,154,65,240,97,234,100,249,186,66,90,238,168,128,177,235,50,81,185,165,217,207,88,80,112,78,135,62,228,247,211});rob(new int[] { 2, 7, 9, 3, 1 });}public static List<Integer> rob(int[] nums) {if (nums == null || nums.length == 0) {return new LinkedList<>();}List<Integer> path1 = new LinkedList<>();List<Integer> path2 = new LinkedList<>();boolean first = true;int[] maxValue = new int[nums.length + 1];maxValue[1] = nums[0];path1.add(1);for (int i = 2; i <= nums.length; i++) {if (maxValue[i - 2] + nums[i - 1] > maxValue[i - 1]) {maxValue[i] = maxValue[i - 2] + nums[i - 1];} else {maxValue[i] = maxValue[i - 1];}if (path1.get(path1.size() - 1) == i - 1) {path2.add(i);if (i == nums.length) {first = false;}} else {path1.add(i);}}if (first) {return path1;} else {return path2;}}
这篇关于house robber follow up的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!