I was solving house robber problem of leetcode using dynamic programming and leetcode is giving me Line 1034: Char 34: runtime error: addition of unsigned offset to 0x6020000000b0 overflowed to 0x6020000000ac (stl_vector.h) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:34
where did i go wrong ?
class Solution {
public:
int solveUtil(int ind, vector<int>& arr, vector<int>& dp){
if(dp[ind]!=-1) return dp[ind];
if(ind==0) return arr[ind];
if(ind<0) return 0;
int pick= arr[ind]+ solveUtil(ind-2, arr,dp);
int nonPick = 0 + solveUtil(ind-1, arr, dp);
return dp[ind]=max(pick, nonPick);
}
int rob(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n,-1);
return solveUtil(n-1,nums, dp);
}
};
Aucun commentaire:
Enregistrer un commentaire