forked from int32bit/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
src.c
55 lines (55 loc) · 1.01 KB
/
src.c
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
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
static inline int max(int a, int b)
{
return a >= b ? a : b;
}
static inline int min(int a, int b)
{
return a <= b ? a : b;
}
int get(int i)
{
return i & 0x1;
}
int robWithoutCircle(int num[], int n) {
int df[2];
int i, result = -1;
if (n <= 0)
return 0;
if (n == 1)
return num[0];
df[0] = num[0];
df[1] = max(df[0], num[1]);
result = max(df[0], df[1]);
for (i = 2; i < n; ++i) {
df[get(i)] = max(df[get(i - 2)] + num[i], df[get(i - 1)]);
result = max(result, df[get(i)]);
}
return result;
}
int rob(int *nums, int n)
{
if (nums == NULL || n <= 0)
return 0;
if (n == 1)
return nums[0];
return max(robWithoutCircle(nums, n - 1), robWithoutCircle(nums + 1, n - 1));
}
void print(int *a, int n)
{
for (int i = 0; i < n; ++i)
printf("%d ", a[i]);
printf("\n");
}
int main(int argc, char **argv)
{
int a[20], n;
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]);
print(a, n);
printf("result = %d\n", rob(a, n));
}
return 0;
}