Skip to content

Commit fde8195

Browse files
committed
house-robber solution
1 parent c5c6b04 commit fde8195

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

house-robber/sungjinwi.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
4+
/*
5+
시간복잡도
6+
7+
이중for문
8+
=> O(N^2)
9+
10+
공간복잡도
11+
12+
dp만큼 malloc
13+
=> O(N)
14+
*/
15+
16+
int rob(int* nums, int numsSize) {
17+
int *dp;
18+
int max_before;
19+
int max = 0;
20+
21+
dp = malloc(numsSize * sizeof(int));
22+
for (int i = 0; i < numsSize; i++)
23+
{
24+
max_before = 0;
25+
for (int j = 0; j < i - 1; j++)
26+
if (dp[j] > max_before)
27+
max_before = dp[j];
28+
dp[i] = nums[i] + max_before;
29+
}
30+
for (int i = 0; i < numsSize; i++)
31+
if (dp[i] > max)
32+
max = dp[i];
33+
free(dp);
34+
return (max);
35+
}

0 commit comments

Comments
 (0)