-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp3.cpp
More file actions
46 lines (35 loc) · 1.48 KB
/
p3.cpp
File metadata and controls
46 lines (35 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "p3.h"
//ASSUMPTIONS:
//A valid list of integer houses will be passed in as a vector
int rob_max_value_(vector<int> houses, int start, unordered_map<int, int> &table) {
//if rob_max_value() was already called with the current start value,
//then return the previously recorded answer
if(table.find(start) != table.end()) {
return table[start];
}
//return 0 if start is past the size of houses
if(start > houses.size() - 1) {
return 0;
}
//if start is at idx of the last hosue, return its value
if(start == houses.size() - 1) {
return houses[start];
}
//first option is to rob the starting house and skip the next house
int option1 = houses[start] + rob_max_value_(houses, start + 2, table);
//second option is to skip the starting house and rob the next hosue
int option2 = rob_max_value_(houses, start + 1, table);
//maxVal will be the most valuable option
int maxVal = max(option1, option2);
//record the return value in the table with start as the key
table[start] = maxVal;
return maxVal;
}
//this is the function to call
int rob_max_value(vector<int> houses) {
unordered_map<int, int> table;
//houses is a vector of housese and their values
//0 is passed in to denote that we want to get the max value starting from house 0
//table is a map used for memoization of the rob_max_value function
return rob_max_value_(houses, 0, table);
}