From 93a4181325b2563ed51c98ae75aecca4d2ed1056 Mon Sep 17 00:00:00 2001 From: kasyap dharanikota <76957823+kasyap1234@users.noreply.github.com> Date: Fri, 25 Nov 2022 17:31:25 +0530 Subject: [PATCH] Create HouseRobber.cpp solved using memoization --- Algorithms/DynamicProgramming/HouseRobber.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Algorithms/DynamicProgramming/HouseRobber.cpp diff --git a/Algorithms/DynamicProgramming/HouseRobber.cpp b/Algorithms/DynamicProgramming/HouseRobber.cpp new file mode 100644 index 0000000..ec035d5 --- /dev/null +++ b/Algorithms/DynamicProgramming/HouseRobber.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int solve(int index, vector &nums, vector &dp){ + if(index==0){ + return nums[index]; + } + if(index<0){ + return 0; + } + if(dp[index]!=-1){ + return dp[index]; + + } + int pick=nums[index] + solve(index-2, nums,dp); + int notPick=solve(index-1, nums,dp); + return dp[index]=max(pick,notPick); + } + int rob(vector& nums) { + int n=nums.size(); + vector dp(n,-1); + + + + return solve(n-1 , nums , dp); + } +};