Skip to content

Commit

Permalink
Merge pull request #666 from sungjinwi/main
Browse files Browse the repository at this point in the history
[suwi] Week1
  • Loading branch information
SamTheKorean authored Dec 15, 2024
2 parents a43334e + fde8195 commit 026e627
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
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)
*/
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);
}
}

0 comments on commit 026e627

Please sign in to comment.