Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions contains-duplicate/sungjinwi.c
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정확히는 모르지만, 혹시 퀵소트에서는 공간복잡도를 사용하지 않나요?

Copy link
Contributor Author

@sungjinwi sungjinwi Dec 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 C 라이브러리의 qsort에 대해 정확히 아는 바가 없어서 시간복잡도와 공간복잡도는 제대로 계산 못 했던거 같아요
찾아보니 퀵소트는 구현방법에 따라서 O(n) 또는 O(1)로 공간복잡도를 가질 수 있지만 C표준에서는 따로 복잡도에 대해 언급된 부분이 없어서 정확한 계산은 못할 것 같아요
심지어 이름만 qsort이고 퀵소트와는 관련도 없을수도 있다고 하네요..ㅠ (해당 사이트의 Note부분)
앞으로 좀 더 찾아보고 사용하는 습관을 들여야겠습니다
좋은 지적 감사드려요!

*/
35 changes: 35 additions & 0 deletions house-robber/sungjinwi.c
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);
}
51 changes: 51 additions & 0 deletions valid-palindrome/sungjinwi.c
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);
}
}
Loading