This is my problem I'm working on: " You are a professional robber planning to rob houses along a street. The houses are numbered 0, 1, 2, ..., and you my assume that the number of houses is at most 10000. Each house has a certain amount of money stashed. The only constraint stopping you from robbing each of them is that adjacent houses have their security systems connected and they will automatically contact the police if two adjacent houses are broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police."
I have to make it work by using dynamic programming with memoization by modifying rob( ), rob_rec(), and introduce a new function that can be called by rob( ). I can't change main( ). Function rob( ) and another function rob_rec( ) solves the problem using dynamic programming with memoization as follows. They should have time complexity O(numHouses).
I would like to achieve an output of this:
Score = 4
Check to verify solution:
Score = 4
Money = 1 2 3 1
House = 1 0 1 0
Score = 12
Check to verify solution:
Score = 12
Money = 2 7 9 3 1
House = 1 0 1 0 1
Score = 19
Check to verify solution:
Score = 19
Money = 10 7 3 9 1
House = 1 0 0 1 0
Score = 605
Check to verify solution:
Score = 605
Money = 72 7 54 23 29 56 32 21 20 64 68 86 70 95 62 59 94 9 63 15
House = 1 0 1 0 0 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0
Below is my current code:
#include <stdlib.h>
#include <stdio.h>
#define HOUSE_SIZE 20
int rob(int* money, int* house, int numHouses);
/* Utility functions */
void clear_houses(int * house, int length); /* Clears array house[] */
void check_houses(int * house, int * money, int length); /* Checks if houses are valid */
void random_fill(int * money, int length); /* Randomly fill array */
void display_array(int* a, int length);
int main()
{
int money1[4] = {1, 2, 3, 1};
int money2[5] = {2, 7, 9, 3, 1};
int money3[5] = {10, 7, 3, 9, 1};
int money4[HOUSE_SIZE];
random_fill(money4, HOUSE_SIZE); /* Fill money4[ ] */
int house[HOUSE_SIZE];
clear_houses(house, 4);
printf("Score = %d\n", rob(money1, house, 4));
check_houses(house, money1, 4);
clear_houses(house, 5);
printf("Score = %d\n", rob(money2, house, 5));
check_houses(house, money2, 5);
clear_houses(house, 5);
printf("Score = %d\n", rob(money3, house, 5));
check_houses(house, money3, 5);
clear_houses(house, HOUSE_SIZE);
printf("Score = %d\n", rob(money4, house, HOUSE_SIZE));
check_houses(house, money4, HOUSE_SIZE);
}
int rob(int* money, int* house, int numHouses)
{
if (numHouses == 0) return 0;
if (numHouses == 1) {
house[0] = 1;
return money[0];
}
int* maxMoney = (int*)malloc(numHouses * sizeof(int));
maxMoney[0] = money[0];
maxMoney[1] = (money[0] > money[1]) ? money[0] : money[1];
for (int i = 2; i < numHouses; i++) {
int robCurrentHouse = maxMoney[i - 2] + money[i];
int skipCurrentHouse = maxMoney[i - 1];
if (robCurrentHouse >= skipCurrentHouse) {
house[i] = 1; // Mark the house as robbed
maxMoney[i] = robCurrentHouse;
} else {
house[i] = 0; // Mark the house as skipped
maxMoney[i] = skipCurrentHouse;
}
}
int result = maxMoney[numHouses - 1];
free(maxMoney);
return result;
}
void display_array(int* a, int length)
{
for (int k = 0; k < length; k++) {
printf("%3d", a[k]);
}
printf("\n");
}
void random_fill(int * money, int length)
{
int state = 11;
for (int k = 0; k < length; k++) {
state = (53 * state + 71) % 97;
money[k] = state;
}
}
void check_houses(int * house, int * money, int length)
{
int check_score = 0;
for (int k = 0; k < length; k++) {
if (house[k] == 1) check_score += money[k];
}
printf("Check to verify solution:\n");
printf(" Score = %d\n", check_score);
printf(" Money = ");
display_array(money, length);
printf(" House = ");
display_array(house, length);
printf("\n");
}
void clear_houses(int * house, int length)
{
for (int i = 0; i < length; i++) {
house[i] = 0;
}
}
I'm having trouble understanding how to update the house array so that it's filled correctly and the rob() function returns the correct value. Can anyone help me?
This was my output:
Score = 4
Check to verify solution:
Score = 3
Money = 1 2 3 1
House = 0 0 1 0
Score = 12
Check to verify solution:
Score = 10
Money = 2 7 9 3 1
House = 0 0 1 0 1
Score = 19
Check to verify solution:
Score = 12
Money = 10 7 3 9 1
House = 0 0 1 1 0
Score = 605
Check to verify solution:
Score = 741
Money = 72 7 54 23 29 56 32 21 20 64 68 86 70 95 62 59 94 9 63 15
House = 0 0 1 0 1 1 1 1 1 1 1 1 0 1 0 1 1 0 1 0