您的位置:首页 > 其它

house robber follow up

2016-08-04 15:08 323 查看
输出抢劫的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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: