Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Method 1:
- There is only two possibilties:
- Either take the elment of don't take the element.
- using a 1-D dp table to store the maximum till the ith house.
- T.C : O(n), S.C : O(n)
- */
- /*
- class Solution{
- public:
- int FindMaxSum(int arr[], int n){
- int dp[n];
- dp[0]=arr[0];
- dp[1]=max(arr[0],arr[1]);
- for(int i=2;i<n;i++){
- dp[i]=max(dp[i-1],dp[i-2]+arr[i]);
- }
- return dp[n-1];
- }
- };
- */
- //We need only two prev value.
- def rob(nums):
- prev2, prev1 = 0, 0
- for num in nums:
- prev2, prev1 = prev1, max(prev1, prev2 + num)
- return prev1
Advertisement
Add Comment
Please, Sign In to add comment