-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #666 from sungjinwi/main
[suwi] Week1
- Loading branch information
Showing
3 changed files
with
123 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,37 @@ | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
|
||
int compare(void const *a, void const *b) | ||
{ | ||
return (*(int *)a - *(int *)b); | ||
} | ||
|
||
bool containsDuplicate(int* nums, int numsSize) { | ||
int i; | ||
|
||
i = 0; | ||
qsort(nums, numsSize, sizeof(int), compare); | ||
while (i < numsSize - 1) | ||
{ | ||
if (nums[i] == nums[i + 1]) | ||
return (1); | ||
i++; | ||
} | ||
return (0); | ||
} | ||
|
||
/* | ||
시간 복잡도 | ||
qsort(퀵소트)를 통해 O(n log n)을 한 번 수행 + while문을 한 번 돌아서 O(n)만큼의 복잡도를 가짐 | ||
최종 시간 복잡도 : O(n log n) | ||
============================ | ||
공간 복잡도 | ||
추가적으로 할당하는 메모리가 없으므로 O(1)의 복잡도를 가진다 | ||
최종 공간 복잡도 : O(1) | ||
*/ |
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,35 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
/* | ||
시간복잡도 | ||
이중for문 | ||
=> O(N^2) | ||
공간복잡도 | ||
dp만큼 malloc | ||
=> O(N) | ||
*/ | ||
|
||
int rob(int* nums, int numsSize) { | ||
int *dp; | ||
int max_before; | ||
int max = 0; | ||
|
||
dp = malloc(numsSize * sizeof(int)); | ||
for (int i = 0; i < numsSize; i++) | ||
{ | ||
max_before = 0; | ||
for (int j = 0; j < i - 1; j++) | ||
if (dp[j] > max_before) | ||
max_before = dp[j]; | ||
dp[i] = nums[i] + max_before; | ||
} | ||
for (int i = 0; i < numsSize; i++) | ||
if (dp[i] > max) | ||
max = dp[i]; | ||
free(dp); | ||
return (max); | ||
} |
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,51 @@ | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include <ctype.h> | ||
#include <stdbool.h> | ||
|
||
/* | ||
문자열 s의 길이를 N이라고 할 때 | ||
시간복잡도 | ||
for문 두 번 돌려서 2N | ||
=> N | ||
================= | ||
공간복잡도 | ||
두 번 복사하면서 2N | ||
=> N | ||
*/ | ||
|
||
bool isPalindrome(char* s) { | ||
char *alnumDup = calloc(strlen(s) + 1, sizeof(char)); | ||
char *revDup; | ||
int j = 0; | ||
|
||
for (int i = 0; s[i]; i++) | ||
{ | ||
if (isalnum(s[i])) | ||
{ | ||
alnumDup[j] = tolower(s[i]); | ||
j++; | ||
} | ||
} | ||
revDup = calloc(strlen(alnumDup) + 1, sizeof(char)); | ||
j = 0; | ||
for (int i = strlen(alnumDup); i; i--) | ||
revDup[j++] = alnumDup[i - 1]; | ||
if (strcmp(alnumDup, revDup)) | ||
{ | ||
free(alnumDup); | ||
free(revDup); | ||
return (false); | ||
} | ||
else | ||
{ | ||
free(alnumDup); | ||
free(revDup); | ||
return (true); | ||
} | ||
} |