File tree Expand file tree Collapse file tree 3 files changed +123
-0
lines changed Expand file tree Collapse file tree 3 files changed +123
-0
lines changed Original file line number Diff line number Diff line change 1+ #include <stdlib.h>
2+ #include <stdbool.h>
3+
4+ int compare (void const * a , void const * b )
5+ {
6+ return (* (int * )a - * (int * )b );
7+ }
8+
9+ bool containsDuplicate (int * nums , int numsSize ) {
10+ int i ;
11+
12+ i = 0 ;
13+ qsort (nums , numsSize , sizeof (int ), compare );
14+ while (i < numsSize - 1 )
15+ {
16+ if (nums [i ] == nums [i + 1 ])
17+ return (1 );
18+ i ++ ;
19+ }
20+ return (0 );
21+ }
22+
23+ /*
24+ 시간 복잡도
25+
26+ qsort(퀵소트)를 통해 O(n log n)을 한 번 수행 + while문을 한 번 돌아서 O(n)만큼의 복잡도를 가짐
27+
28+ 최종 시간 복잡도 : O(n log n)
29+
30+ ============================
31+
32+ 공간 복잡도
33+
34+ 추가적으로 할당하는 메모리가 없으므로 O(1)의 복잡도를 가진다
35+
36+ 최종 공간 복잡도 : O(1)
37+ */
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ #include <string.h>
2+ #include <stdlib.h>
3+ #include <ctype.h>
4+ #include <stdbool.h>
5+
6+ /*
7+ 문자열 s의 길이를 N이라고 할 때
8+
9+ 시간복잡도
10+
11+ for문 두 번 돌려서 2N
12+
13+ => N
14+ =================
15+ 공간복잡도
16+
17+ 두 번 복사하면서 2N
18+
19+ => N
20+ */
21+
22+ bool isPalindrome (char * s ) {
23+ char * alnumDup = calloc (strlen (s ) + 1 , sizeof (char ));
24+ char * revDup ;
25+ int j = 0 ;
26+
27+ for (int i = 0 ; s [i ]; i ++ )
28+ {
29+ if (isalnum (s [i ]))
30+ {
31+ alnumDup [j ] = tolower (s [i ]);
32+ j ++ ;
33+ }
34+ }
35+ revDup = calloc (strlen (alnumDup ) + 1 , sizeof (char ));
36+ j = 0 ;
37+ for (int i = strlen (alnumDup ); i ; i -- )
38+ revDup [j ++ ] = alnumDup [i - 1 ];
39+ if (strcmp (alnumDup , revDup ))
40+ {
41+ free (alnumDup );
42+ free (revDup );
43+ return (false);
44+ }
45+ else
46+ {
47+ free (alnumDup );
48+ free (revDup );
49+ return (true);
50+ }
51+ }
You can’t perform that action at this time.
0 commit comments