Skip to content

Commit

Permalink
House robber
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Ma <[email protected]>
  • Loading branch information
begeekmyfriend committed Dec 19, 2017
1 parent 409d00b commit 048ee99
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 198_house_robber/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test robber.c
37 changes: 37 additions & 0 deletions 198_house_robber/robber.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static inline int max(int a, int b)
{
return a > b ? a : b;
}

static int rob(int* nums, int numsSize)
{
int i;
int **money = malloc((numsSize + 1) * sizeof(int *));
for (i = 0; i < numsSize + 1; i++) {
money[i] = malloc(2 * sizeof(int));
memset(money[i], 0, 2 * sizeof(int));
}

for (i = 1; i <= numsSize; i++) {
int cash = nums[i - 1];
money[i][0] = max(money[i - 1][0], money[i - 1][1]);
money[i][1] = money[i - 1][0] + cash;
}

return max(money[numsSize][0], money[numsSize][1]);
}

int main(int argc, char **argv)
{
int i, count = argc - 1;
int *nums = malloc(count * sizeof(int));
for (i = 0; i < count; i++) {
nums[i] = atoi(argv[i + 1]);
}
printf("%d\n", rob(nums, count));
return 0;
}
2 changes: 2 additions & 0 deletions 213_house_robber_ii/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test robber.c
48 changes: 48 additions & 0 deletions 213_house_robber_ii/robber.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static inline int max(int a, int b)
{
return a > b ? a : b;
}

static int _rob(int* nums, int numsSize)
{
int i;
int **money = malloc((numsSize + 1) * sizeof(int *));
for (i = 0; i < numsSize + 1; i++) {
money[i] = malloc(2 * sizeof(int));
memset(money[i], 0, 2 * sizeof(int));
}

for (i = 1; i <= numsSize; i++) {
int cash = nums[i - 1];
money[i][0] = max(money[i - 1][0], money[i - 1][1]);
money[i][1] = money[i - 1][0] + cash;
}

return max(money[numsSize][0], money[numsSize][1]);
}

static int rob(int* nums, int numsSize)
{
if (numsSize == 0) {
return 0;
} else if (numsSize == 1) {
return nums[0];
} else {
return max(_rob(nums + 1, numsSize - 1), _rob(nums, numsSize - 1));
}
}

int main(int argc, char **argv)
{
int i, count = argc - 1;
int *nums = malloc(count * sizeof(int));
for (i = 0; i < count; i++) {
nums[i] = atoi(argv[i + 1]);
}
printf("%d\n", rob(nums, count));
return 0;
}

0 comments on commit 048ee99

Please sign in to comment.