LeetCode 動態規劃 House Robber 習題
關於我的 Leetcode 題目解答,程式碼前往 Github:https://github.com/chenxiangcyr/leetcode-answers
LeetCode題目:198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
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.
class Solution {
public int rob(int[] nums) {
// one iteration solution
if(nums.length == 0) {
return 0;
}
int[] profit = new int[nums.length];
profit[0] = nums[0];
for(int i = 1; i < nums.length; i++) {
// assume rob house i
int profit1 = nums[i] + (i > 1 ? profit[i - 2] : 0);
// assume not rob house i
int profit2 = profit[i - 1];
profit[i] = Math.max(profit1, profit2);
}
return profit[nums.length - 1];
}
}
LeetCode題目:213. House Robber II
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.
class Solution {
public int rob(int[] nums) {
// one iteration solution
if(nums.length == 0) {
return 0;
}
if(nums.length == 1) {
return nums[0];
}
int profitIncludeFirstHouse = rob(nums, 0, nums.length - 2);
int profitExcludeFirstHouse = rob(nums, 1, nums.length - 1);
return Math.max(profitIncludeFirstHouse, profitExcludeFirstHouse);
}
public int rob(int[] nums, int start, int end) {
if(start >= end) {
return nums[start];
}
int[] profit = new int[end - start + 1];
profit[0] = nums[start];
for(int i = 1; i < profit.length; i++) {
// assume rob house i
int profit1 = nums[start + i] + (i > 1 ? profit[i - 2] : 0);
// assume not rob house i
int profit2 = profit[i - 1];
profit[i] = Math.max(profit1, profit2);
}
return profit[profit.length - 1];
}
}
LeetCode題目:337. House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
class Solution {
public int rob(TreeNode root) {
if(root == null) {
return 0;
}
// If not rob root, then rob left sub tree and right sub tree
int profit1 = rob(root.left) + rob(root.right);
// If rob root
int profit2 = root.val;
if(root.left != null) {
profit2 = profit2 + rob(root.left.left) + rob(root.left.right);
}
if(root.right != null) {
profit2 = profit2 + rob(root.right.left) + rob(root.right.right);
}
return Math.max(profit1, profit2);
}
}
相關文章
- 動態規劃專題之----198. House Robber動態規劃
- 動態規劃專題之----213. House Robber II動態規劃
- LeetCode-House RobberLeetCode
- Leetcode 198 House RobberLeetCode
- Leetcode 213 House Robber IILeetCode
- LeetCode-House Robber IILeetCode
- LeetCode-House Robber IIILeetCode
- 【LeetCode】House Robber III(337)LeetCode
- leetcode題解(動態規劃)LeetCode動態規劃
- 動態規劃練習題動態規劃
- Uber面試題 | 房屋竊賊 House Robber II面試題
- [leetcode] 動態規劃(Ⅰ)LeetCode動態規劃
- [LeetCode] 動態規劃題型總結LeetCode動態規劃
- leetcode:動態規劃( hard )LeetCode動態規劃
- LeetCode:動態規劃+貪心題目整理LeetCode動態規劃
- 好題——動態規劃動態規劃
- 動態規劃專題動態規劃
- 動態規劃題單動態規劃
- Leetcode 題解演算法之動態規劃LeetCode演算法動態規劃
- LeetCode總結,動態規劃問題小結LeetCode動態規劃
- leetcode總結——動態規劃LeetCode動態規劃
- LeetCode動態規劃總結LeetCode動態規劃
- 【LeetCode】Word Break 動態規劃LeetCode動態規劃
- 動態規劃練習 6動態規劃
- 動態規劃解題方法動態規劃
- 動態規劃做題思路動態規劃
- 【leetcode】741 摘櫻桃(動態規劃)LeetCode動態規劃
- leetcode-動態規劃總結LeetCode動態規劃
- 【LeetCode】Word Break II 動態規劃LeetCode動態規劃
- [LeetCode解題] -- 動態規劃二 [ 子串、子序列問題 ]LeetCode動態規劃
- 【動態規劃(一)】動態規劃基礎動態規劃
- Leetcode 題解系列 -- 股票的最大利潤(動態規劃)LeetCode動態規劃
- 整數劃分問題(動態規劃)動態規劃
- 力扣練習-動態規劃力扣動態規劃
- 動態規劃學習筆記動態規劃筆記
- 演算法刷題:LeetCode中常見的動態規劃題目演算法LeetCode動態規劃
- Leetcode 編輯距離(動態規劃)LeetCode動態規劃
- [leetcode 1235] [動態規劃]LeetCode動態規劃