forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Leo Ma <[email protected]>
- Loading branch information
1 parent
409d00b
commit 048ee99
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test robber.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test robber.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |