-
-
Notifications
You must be signed in to change notification settings - Fork 245
[suwi] Week1 #666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[suwi] Week1 #666
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f5c94ae
containsDuplicate solution
sungjinwi 4b99ac7
Docs : complexity calc for containDuplicate
sungjinwi 9e96796
Merge branch 'DaleStudy:main' into main
sungjinwi d90dfc2
valid-palindrome solution && complexity calc
sungjinwi c5c6b04
Merge branch 'DaleStudy:main' into main
sungjinwi fde8195
house-robber solution
sungjinwi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
정확히는 모르지만, 혹시 퀵소트에서는 공간복잡도를 사용하지 않나요?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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부분)
앞으로 좀 더 찾아보고 사용하는 습관을 들여야겠습니다
좋은 지적 감사드려요!