house robber follow up

来源:互联网 发布:开淘宝衣服货源怎么找 编辑:程序博客网 时间:2024/06/11 05:52

输出抢劫的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;}}


0 0
原创粉丝点击