Stickler Thief | Practice | GeeksforGeeks

Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it.
Example 1:
Input:
n = 6
a[] = {5,5,10,100,10,5}
Output: 110
Explanation: 5+100+5=110
Example 2:
Input:
n = 3
a[] = {1,2,3}
Output: 4
Explanation: 1+3=4
Your Task:
Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules
Expected Time Complexity:O(N).
Expected Space Complexity:O(N).
Constraints:
1 ≤ n ≤ 104
1 ≤ a[i] ≤ 104
+1
kshitij14106 hours ago
 vector<int>dp(n+1,-1);       dp[0]=0;       dp[1]=arr[0];              for(int i=2;i<=n;i++)       {           dp[i]=max(dp[i-1], arr[i-1]+ dp[i-2]);       }                     return dp[n];
0
nirmal119140831 week ago
int dp[100001];   int solve(int index,vector<int> v,int N)   {    if(index>=N)   {       return 0;   }   if(dp[index]!=-1)   {       return dp[index];   }       int left_sum= v[index]+solve(index+2,v,N);      int right_sum=solve(index+1,v,N);       return dp[index]=max(left_sum,right_sum);   }   int FindMaxSum(int arr[], int n)   {       // Your code here       vector<int> v;       memset(dp,-1,100001);       for(int i=0;i<n;i++)       {           v.push_back(arr[i]);       }       int N=v.size();       return solve(0,v,N);   }
// why my time limit is getting exceeded
0
abhiswc291 week ago
Python Memoized 

class Solution:  
    def FindMaxSum(self,a, n):
        dp = [-1 for _ in range(n+1)]
    
        def solve(arr, n):
            if n < 0:
                return 0
            if dp[n] != -1:
                return dp[n]
            t1 = arr[n]+solve(arr, n-2)
            t2 = solve(arr, n-1)
            dp[n] = max(t1, t2)
            return dp[n]
        return solve(a, n-1)
+1
manasbajpai163 weeks ago
class Solution
{
    public int FindMaxSum(int arr[], int n)
    {
        int inc = arr[0], excl = 0;
        for(int i = 1; i < n; i++)
        {
            int ninc = excl + arr[i];
            int nexcl = Math.max(inc , excl);
            
            inc = ninc;
            excl = nexcl;
        }
        
        return Math.max(inc , excl);
    }
}
JAVA Solution
 
+1
kashyapjhon3 weeks ago
C++ Solution Time=(0.3/1.4) EASY :
int help(vector<int> &dp,int arr[],int ind){       if(ind==0){           return arr[ind];       }       if(ind<0){           return 0;       }       if(dp[ind]!=-1){           return dp[ind];       }       int takemoney= help(dp,arr,ind-2)+arr[ind];       int skiphouse= help(dp,arr,ind-1);       return dp[ind]=max(takemoney,skiphouse);   }   int FindMaxSum(int arr[], int n)   {       // Your code here       vector<int> dp(n+1,-1);       int ind=n-1;       return help(dp,arr,ind);   }
+5
omkarg14174 weeks ago
simple iterative solution:
int FindMaxSum(int a[], int n)
    {
        vector<int> dp(n);
        dp[0] = a[0];
        dp[1] = max(a[1], a[0]);
        
        for(int i = 2; i < n; ++i) {
            dp[i] = max(a[i] + dp[i-2], dp[i-1]);
        }
        
        return *max_element(dp.begin(), dp.end());
    }
+1
nikkipro94 weeks ago
               int prevAmount = 0;       int sum = 0;   bool isNumInBasePlace = true;
   for (int i = 0; i < n; i++)   {          if (prevAmount != arr[i])       {           if (isNumInBasePlace)           {               sum += arr[i];           }           isNumInBasePlace = isNumInBasePlace?false:true;       }
       prevAmount = arr[i];          }
   return sum;
0
dineshsountyal1 month ago
Can anyone tell me which case i am missing here.
 
       l_even = 0       l_odd = 0       for i in range(n):           if i%2==0:               l_even+=a[i]           else:               l_odd+=a[i]              if l_even >= l_odd:           return l_even       else:           return l_odd       
 
 
 
+3
deskhell1 month ago
class Solution
{
    public:
    //Function to find the maximum money the thief can get.
    int FindMaxSum(int arr[], int n)
    {
        int inc = 0, exc = 0;
        for(int i = 0; i < n; i++){
            int temp = inc;
            inc = arr[i] + exc;
            exc = max(temp, exc);
        }
        return max(inc, exc);
    }
};
0
deskhell
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.