Skip to content

Commit 0adefcb

Browse files
committed
feature: dp house robber 2 solution added
1 parent 562de8b commit 0adefcb

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
#include<vector>
3+
using namespace std;
4+
5+
/*
6+
Pattern 1
7+
Linear Recurrence
8+
9+
Description
10+
You are given an array arr[] which represents houses arranged in a circle, where each house has a certain value. A thief aims to maximize the total stolen value without robbing two adjacent houses.
11+
Determine the maximum amount the thief can steal.
12+
13+
Note: Since the houses are in a circle, the first and last houses are also considered adjacent.
14+
15+
*/
16+
17+
namespace HouseRobber2
18+
{
19+
class DynamicProgramming
20+
{
21+
private:
22+
int MaxLootRecursive(size_t house, vector<int>& houseValues);
23+
int MaxLootDp(size_t firstHouse, size_t lastHouse, vector<int>& houseValues);
24+
public:
25+
int RecursiveMaximumLoot(vector<int>& houseValues);
26+
int DpMaximumLoot(vector<int>& houseValues);
27+
};
28+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "../../include/0005_DynamicProgramming/0006_HouseRobber2.h"
2+
3+
namespace HouseRobber2
4+
{
5+
int DynamicProgramming::MaxLootRecursive(size_t house, vector<int>& houseValues)
6+
{
7+
if (house <= 0)
8+
{
9+
return 0;
10+
}
11+
12+
if (house == 1)
13+
{
14+
return houseValues[0];
15+
}
16+
17+
int pickCurrentHouse = houseValues[house - 1] + this->MaxLootRecursive(house - 2, houseValues);
18+
int dropCurrentHouse = this->MaxLootRecursive(house - 1, houseValues);
19+
20+
return max(pickCurrentHouse, dropCurrentHouse);
21+
}
22+
23+
int DynamicProgramming::MaxLootDp(size_t firstHouse, size_t lastHouse, vector<int>& houseValues)
24+
{
25+
int totalNumberOfHouses = lastHouse - firstHouse + 1;
26+
27+
if (totalNumberOfHouses == 0)
28+
{
29+
return 0;
30+
}
31+
32+
if (totalNumberOfHouses == 1)
33+
{
34+
return houseValues[firstHouse];
35+
}
36+
37+
vector<int> dp(totalNumberOfHouses, 0);
38+
39+
dp[0] = houseValues[firstHouse];
40+
dp[1] = max(houseValues[firstHouse], houseValues[firstHouse + 1]);
41+
42+
for (size_t i = 2; i < totalNumberOfHouses; i++)
43+
{
44+
dp[i] = max(houseValues[firstHouse + i] + dp[i - 2], dp[i - 1]);
45+
}
46+
47+
return dp[totalNumberOfHouses - 1];
48+
}
49+
50+
int DynamicProgramming::RecursiveMaximumLoot(vector<int>& houseValues)
51+
{
52+
if (houseValues.size() == 0)
53+
{
54+
return 0;
55+
}
56+
57+
if (houseValues.size() == 1)
58+
{
59+
return houseValues[0];
60+
}
61+
62+
size_t totalNumberOfHouses = houseValues.size()-1;
63+
64+
// Case 1: Exclude last house.
65+
vector<int> pickFirstHouse(houseValues.begin(), houseValues.end() - 1);
66+
67+
// Case 2: Exlcude first house.
68+
vector<int> pickLastHouse(houseValues.begin() + 1, houseValues.end());
69+
70+
return max(this->MaxLootRecursive(totalNumberOfHouses, pickFirstHouse), this->MaxLootRecursive(totalNumberOfHouses, pickLastHouse));
71+
}
72+
73+
int DynamicProgramming::DpMaximumLoot(vector<int>& houseValues)
74+
{
75+
size_t totalNumberOfHouses = houseValues.size();
76+
77+
if (totalNumberOfHouses == 0)
78+
{
79+
return 0;
80+
}
81+
82+
if (totalNumberOfHouses == 1)
83+
{
84+
return houseValues[0];
85+
}
86+
87+
// Case 1: Exclude last house.
88+
int pickFirstHouse = this->MaxLootDp(0, totalNumberOfHouses - 2, houseValues);
89+
90+
// Case 2: Exlcude first house.
91+
int pickLastHouse = this->MaxLootDp(1, totalNumberOfHouses - 1, houseValues);
92+
93+
return max(pickFirstHouse, pickLastHouse);
94+
}
95+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include<gtest/gtest.h>
2+
#include "../../include/0005_DynamicProgramming/0006_HouseRobber2.h"
3+
4+
namespace HouseRobber2
5+
{
6+
TEST(HouseRobber2, RecursionTest01)
7+
{
8+
// Arrange
9+
DynamicProgramming dp;
10+
vector<int> houseValues = { 2, 2, 3, 1, 2 };
11+
int expectedMaximumLoot = 5;
12+
13+
// Act
14+
int actualMaximumLoot = dp.RecursiveMaximumLoot(houseValues);
15+
16+
// Assert
17+
ASSERT_EQ(expectedMaximumLoot, actualMaximumLoot);
18+
}
19+
20+
TEST(HouseRobber2, DpTest01)
21+
{
22+
// Arrange
23+
DynamicProgramming dp;
24+
vector<int> houseValues = { 2, 2, 3, 1, 2 };
25+
int expectedMaximumLoot = 5;
26+
27+
// Act
28+
int actualMaximumLoot = dp.DpMaximumLoot(houseValues);
29+
30+
// Assert
31+
ASSERT_EQ(expectedMaximumLoot, actualMaximumLoot);
32+
}
33+
34+
TEST(HouseRobber2, DpTest02)
35+
{
36+
// Arrange
37+
DynamicProgramming dp;
38+
vector<int> houseValues = { 9, 1, 8, 2 };
39+
int expectedMaximumLoot = 17;
40+
41+
// Act
42+
int actualMaximumLoot = dp.DpMaximumLoot(houseValues);
43+
44+
// Assert
45+
ASSERT_EQ(expectedMaximumLoot, actualMaximumLoot);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)