摘要:你不能連著偷兩家因為這樣會觸發(fā)警報系統(tǒng)。現(xiàn)在有一個數(shù)組存放著每一家中的可偷金額,問可以偷的最大金額為多少這里考驗了動態(tài)編程的思想。動態(tài)編程要求我們將問題一般化,然后再找到初始情況開始這個由一般到特殊的計算過程。
House Robber I
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.
假設你現(xiàn)在是一個小偷,想要一整條街上的人家。你不能連著偷兩家因為這樣會觸發(fā)警報系統(tǒng)。現(xiàn)在有一個nums數(shù)組存放著每一家中的可偷金額,問可以偷的最大金額為多少?
這里考驗了動態(tài)編程的思想。動態(tài)編程要求我們將問題一般化,然后再找到初始情況開始這個由一般到特殊的計算過程。
我們先考慮一般情景,假設已經(jīng)經(jīng)過了i-1戶人家,現(xiàn)在決定是否要偷第i家,
有兩種選擇:偷 or 不偷
如果偷的話,就意味著前一戶人家不能偷,因此我們需要知道前一戶人家不偷的話我們可以獲得的最大收益,也就是rob=notRub+nums[i]
如果不偷的話,就意味著著前一戶人家可以是偷過或者沒有偷過,那么我們只需要保留二者的最大值就可以了,也就是notRob = max(rob, notRob)
在這個基礎上,我們來尋找一般情況,也就是第一次作案時候的選擇,則rob=nums[0],notRob=0
現(xiàn)在來舉一個具體的例子,假設我現(xiàn)在nums的值為[4,2,3,9],我們按照上面的思路,將rob和notRob分別初始化為4和0,則rob和notRob的變化如下
rob : notRob
4 : 0
2 : 4
7 : 4(max(2,4))
13 : 7
則可以獲得最大值為13
代碼如下:
public int rob(int[] nums) { if(nums==null || nums.length ==0) return 0; //搶劫了前一個房子可以獲得的最大收入 int rob = nums[0]; //沒有搶劫前一個房子可以獲得的最大收入 int notRob = 0; for(int i = 1 ; iHouse 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.和第一題需求的區(qū)別在于,這個街區(qū)的房子為環(huán)形的。也就是說首尾的房子也只能二選一。這個問題其實可以直接從第一個問題升華。我們可以把這個問題直接拆解為選擇第一個房子而忽略最后一個房子可以獲得的最大收入,以及選擇最后一個房子而忽略第一個房子可以獲得的最大收入。
至于如果最好的情況是二者都不選的話,那么前一類和后一類都會覆蓋這種情況,所以說這種分類可以覆蓋全部的情況。代碼如下:
public int rob(int[] nums) { if(nums==null || nums.length==0) return 0; if(nums.length==1) return nums[0]; return Math.max(rob(nums, 0, nums.length-2),rob(nums, 1, nums.length-1)); } public int rob(int[] nums, int low, int high){ int rob = 0; int notRob = 0; while(low<=high){ int temp = rob; rob = notRob + nums[low]; notRob = Math.max(temp, notRob); low++; } return Math.max(rob, notRob); }
想要了解更多開發(fā)技術,面試教程以及互聯(lián)網(wǎng)公司內推,歡迎關注我的微信公眾號!將會不定期的發(fā)放福利哦~
文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/70899.html
摘要:但是任何臨近的兩個房子被偷就會觸發(fā)警報。要求我們求出在不觸發(fā)警報的情況下偷到的最多的錢。每個房子里的錢通過輸入的數(shù)組表示。 題目詳情 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only...
摘要:注意對邊界條件的判斷,是否非空,是否長度為 House Robber I Problem 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 y...
摘要:動態(tài)規(guī)劃復雜度時間空間思路一般來說,給定一個規(guī)則,讓我們求任意狀態(tài)下的解,都是用動態(tài)規(guī)劃。另外我們可以做一點優(yōu)化,本來我們是要用一個數(shù)組來保存之前的結果的。所以我們分別算出這兩個條件下的最大收益,然后取更大的就行了。可以復用的代碼。 House Robber I You are a professional robber planning to rob houses along a ...
摘要:因為取了隊首就不能取隊尾,所以對數(shù)組循環(huán)兩次,一個從取到,一個從取到,比較兩次循環(huán)后隊尾元素,取較大者。注意,要先討論當原數(shù)組位數(shù)小于時的情況。 Problem After robbing those houses on that street, the thief has found himself a new place for his thievery so that he wi...
摘要:復雜度思路對于每一個位置來說,考慮兩種情況分別對和再進行計算。用對已經(jīng)計算過的進行保留,避免重復計算。 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, calle...
閱讀 2053·2021-11-22 13:52
閱讀 976·2021-11-17 09:33
閱讀 2708·2021-09-01 10:49
閱讀 2841·2019-08-30 15:53
閱讀 2659·2019-08-29 16:10
閱讀 2432·2019-08-29 11:31
閱讀 1343·2019-08-26 11:40
閱讀 1866·2019-08-26 10:59