LeetCode 198. House Robber
題目大意:
去搶一列房子,但是不能搶相鄰地房子,現在希望搶的價值最多。也就是說在一串陣列中找到一個子串,和最大,但是不能不能取兩個連續的數字。
思路:
維護一個數組,記錄每個位置上及以前的最大值,即記錄第個位置及以前地數字地最大和。由於不能取連續的兩個數字,因此如果要取第個位置地數字,則第個位置是不能取的;也可以不取第個位置的數字,則可以保留第個位置的值。
這樣在以上兩種情況中取更大值就好了。即
還有一個問題,就是陣列的長度。存在特殊情況,有可能陣列為空,則最大值就是0;有可能陣列就一個數字,則最大值就是這個數字;有可能陣列只有兩個數字,最大值就是兩者最大。一般情況用上面的方法就好,特殊情況特判一下就好。
程式碼如下,時間複雜度為, 空間複雜度:
class Solution {
public:
int rob(vector<int>& nums) {
int dp[10005];
int length = nums.size() ;
if(length == 0){
return 0;
}
if(length == 1){
return nums[0];
}
if(length == 2){
return max(nums[0], nums[1]);
}
if(length > 2){
dp[0] = nums[0];
dp[1] = max(nums[0], nums[1]);
for (int i = 2; i < length; i++){
dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]);
// printf("dp %d\n", dp[i]);
}
return dp[length - 1];
}
}
};
當然可以看到,也就用了這幾個數字。因此可以不用存陣列,用三個數字就行,減少空間複雜度。
相關推薦
leetcode 198. House Robber (Easy)
返回 一次 lee href tar 最大的 保存 題意 效率 https://leetcode.com/problems/house-robber/ 題意: 一維數組,相加不相鄰的數組,返回最大的結果。 思路: 一開始思路就是DP,用一維數組保存dp[i]保存如果偷第
LeetCode 198.House Robber (打家劫舍)
題目描述: 你是一個專業的小偷,計劃偷竊沿街的房屋。每間房內都藏有一定的現金,影響你偷竊的唯一制約因素就是相鄰的房屋裝有相互連通的防盜系統,如果兩間相鄰的房屋在同一晚上被小偷闖入,系統會自動報警。 給定一個代表每個房屋存放金額的非負整數陣列,計算你在不觸動警報裝置的情況下,能夠偷竊到的最高金
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 st
[leetcode]198. House Robber
class Solution { public int rob(int[] nums) { if(nums.length==0)return 0; int[] memo=new int[nums.length+1]; memo[
LeetCode-198. House Robber
0.原題 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only cons
轉 小白刷LeetCode 198.house robber
題目: ou are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constr
動態規劃Leetcode 198 House Robber
Leetcode 198 House Robber Problem Description: 偷東西:不能偷相鄰房子的東西,不然會觸發報警。要求在不被抓的前提下偷到東西的價值最大。 具體的題目資訊: https://leetcode.com/problems/hou
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
LeetCode 198. House Robber
題目大意: 去搶一列房子,但是不能搶相鄰地房子,現在希望搶的價值最多。也就是說在一串陣列中找到一個子串,和最大,但是不能不能取兩個連續的數字。 思路: 維護一個數組dpdpdp,記錄每個位置上及以前的最
leetcode 198.House Robber-打家劫舍|動態規劃
【思路-Java、Python】遞迴實現 考查動態規劃,基本思路是當前節點處最大值curMax = Math.max(curMax, curPrePreMax + cur)。舉個例子[3, 2,
LeetCode-198. House Robber (JAVA)尋找陣列不相鄰組合最大值DP
198. House Robber You are a professional robber planning to rob houses along a street. Each house
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 constr
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 constrai
【LeetCode】198. House Robber
class Solution: # 遞迴法 def rob(self, nums): """ :type nums: List[int]
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 co
【LeetCode-面試演算法經典-Java實現】【198-House Robber(搶劫犯)】
原題 You are a professional robber planning to rob houses along a street. Each house h
198. House Robber Leetcode Python
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constrai
198. House Robber
mine urn con sent use constrain clas imu ping You are a professional robber planning to rob houses along a street. Each house has a certa
【easy】198. House Robber
tro pos color and 很好 二叉樹 house vector ++ 題目一: 一個極其簡單的動態規劃。 class Solution { public: int rob(vector<int>& nums) { i
[leetcode]337. House Robber III
[leetcode]337. House Robber III Analysis fighting!!!—— [每天刷題並不難0.0] The thief has found himself a new place for his thievery again