Skip to content

Commit f3a9a4a

Browse files
committed
add : solution house-robber
1 parent 593e06a commit f3a9a4a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

house-robber/aa601.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
int rob(int* nums, int numsSize) {
2+
int *dp;
3+
int max;
4+
int i;
5+
int j;
6+
7+
// dp에 nums 복사
8+
dp = malloc(sizeof(int) * numsSize);
9+
if (!dp)
10+
return (0);
11+
i = -1;
12+
while (++i < numsSize) {
13+
dp[i] = nums[i];
14+
}
15+
16+
// dp 시작
17+
i = 0;
18+
j = 0;
19+
while (++i < numsSize) {
20+
j = 0;
21+
max = dp[i];
22+
while (j + 1 < i) {
23+
if (max < dp[i] + dp[j])
24+
max = dp[i] + dp[j];
25+
++j;
26+
}
27+
dp[i] = max;
28+
}
29+
30+
// dp 배열의 최종 max값 리턴
31+
i = -1;
32+
max = dp[0];
33+
while (++i < numsSize) {
34+
if (max < dp[i])
35+
max = dp[i];
36+
}
37+
free(dp);
38+
return (max);
39+
}
40+

0 commit comments

Comments
 (0)