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);
}
}
相關文章
- Leetcode 198 House RobberLeetCode
- Leetcode 213 House Robber IILeetCode
- leetcode題解(動態規劃)LeetCode動態規劃
- [leetcode] 動態規劃(Ⅰ)LeetCode動態規劃
- [LeetCode] 動態規劃題型總結LeetCode動態規劃
- 337-House Robber III
- 動態規劃練習題動態規劃
- [leetcode 1235] [動態規劃]LeetCode動態規劃
- LeetCode:動態規劃+貪心題目整理LeetCode動態規劃
- leetcode總結——動態規劃LeetCode動態規劃
- Leetcode 題解演算法之動態規劃LeetCode演算法動態規劃
- leetcode-動態規劃總結LeetCode動態規劃
- [LeetCode解題] -- 動態規劃二 [ 子串、子序列問題 ]LeetCode動態規劃
- Leetcode 題解系列 -- 股票的最大利潤(動態規劃)LeetCode動態規劃
- 動態規劃專題動態規劃
- 好題——動態規劃動態規劃
- 動態規劃題單動態規劃
- Leetcode 編輯距離(動態規劃)LeetCode動態規劃
- 【leetcode】741 摘櫻桃(動態規劃)LeetCode動態規劃
- 演算法刷題:LeetCode中常見的動態規劃題目演算法LeetCode動態規劃
- LeetCode中動態規劃題解合集(根據難易程度))LeetCode動態規劃
- 動態規劃做題思路動態規劃
- 動態規劃解題方法動態規劃
- LeetCode 分割回文串II(動態規劃)LeetCode動態規劃
- LeetCode入門指南 之 動態規劃思想LeetCode動態規劃
- LeetCode 343. 整數拆分--動態規劃LeetCode動態規劃
- 【LeetCode】55. 跳躍遊戲 (動態規劃)LeetCode遊戲動態規劃
- 力扣練習-動態規劃力扣動態規劃
- 動態規劃學習筆記動態規劃筆記
- 動態規劃 擺花 題解動態規劃
- 【動態規劃】揹包問題動態規劃
- 我的動態規劃題單動態規劃
- 做題記錄 --- 動態規劃動態規劃
- 揹包問題----動態規劃動態規劃
- 動態規劃之子序列問題動態規劃
- 德魯週記10--15天從0開始刷動態規劃(leetcode動態規劃題目型別總結)動態規劃LeetCode型別
- 動態規劃動態規劃
- [leetcode初級演算法]動態規劃總結LeetCode演算法動態規劃