Able to get result on vscode but not on leetcode 198. House Robber

Viewed 13

This is my soln can anyone help me. I am getting heap-buffer-overflow error on leetcode 198. House Robber.

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int solve(vector<int>& nums,int n,vector<int>& dp){
    if(n<0){
        return 0;
    }
    if(n==0){
        return nums[0];
    }
    if(dp[n]!=-1){
        return dp[n];
    }
    int incl=solve(nums,n-2,dp)+nums[n];
    int excl=solve(nums,n-1,dp)+0;
    dp[n]=max(incl,excl);
    return dp[n];
    }
int rob(vector<int>& nums) {
    int n=nums.size();
    vector<int> dp(n+1,-1);
    return solve(nums,n,dp);
    }
int main(){
 vector<int> t={2,7,9,3,1};
 cout<<rob(t);

return 0;
}

I have written this code in vscode and its working fine;

0 Answers
Related