House robber arra doubt

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.-QUESTION

int helper(vector nums)
{

int n = nums.size();
int dp[n];
memset(dp,0,sizeof(dp));

if (n ==0)
    return 0;

if (n == 1)
    return dp[0];

if (n == 2)
    return max(dp[0], dp[1]);


dp[0]= nums[0];
dp[1]= max(nums[0], nums[1]);

for (int i = 2;i <n; i++)
    dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]);

return dp[n-1];      

}
class Solution {
public:
int rob(vector& nums) {

return helper(nums);
}

};

why does it give run time error,why cannot array be declared with size of n???

Include the helper function within the class solution under public and then check.
If it still does not work then please save your code on ide.codingblocks.com and then share its link.

https://ide.codingblocks.com/s/253859,still showing the error…Runtime Error Message: Line 32: Char 8: runtime error: variable length array bound evaluates to non-positive value 0 (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:41:8 Last executed input: [](this is the error)

Check now. Variable length array is not allowed by all compilers in C++. So better use a vector to avoid confusion. Or if you want to use an array, then allocate memory to the dp array according to the constraints from before.

i duely get the vector approach,that is an alternative but could you tell me more about why doesnot variable length array get accepted…like in this i even tried passing the array form the rob function but same error,i even tried dynamic allocation so would you be able to amke changes for the array int this code only?

Remember that you can declare a variable sized array but you cannot initialize it with some values at the same time.
So memset or other ways of initialization of variable sized array will not work.

check the comments

will declaring an array dynamically not help?? if i want to initialise the array as well,what is the option except vectors?

Option is that, by looking at the constraints, you can get to know the maximum size of the dp table.
Say if the maximum value of n is 10000. Then you can declare dp[10000] and initialize it as you want.

will dynamic declaration of array help in initialising variable length array?

Yes that will also work.
You can through these and other stack overflow articles by googling your query


i tried using dynamic allocation in helper function but again it wasnot able to initalise,could you show in code how can i do that in helpr function?

Run the code and check. This must not give an error.