-
Notifications
You must be signed in to change notification settings - Fork 4
[JooKangsan] 2025.02.13 #42
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
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
19ee12b
fix: 에러 수정
JooKangsan 1e18910
Merge main
JooKangsan c5da3ed
문자열 뒤집기 / 기초
JooKangsan 488872b
Ctrl Z / 기초
JooKangsan ee8476f
크레인 인형뽑기 / 중급
JooKangsan ccfa363
다트게임 / 중급
JooKangsan fb13521
Merge main
JooKangsan 952ddb9
순서쌍의 갯수 / 기초
JooKangsan d7e9a27
점의 위치 구하기 / 기초
JooKangsan 42d696f
로그인 성공? / 기초
JooKangsan 3ee8e31
이상한 정렬 / 기초
JooKangsan 59443db
카드 뭉치 / 기초
JooKangsan ad8b945
feat: md 파일 추가
JooKangsan 3ed157c
공원 산책 / 중급
JooKangsan 8b2f46f
햄버거 만들기 / 중급
JooKangsan 4adabcc
Merge 'origin/main'
JooKangsan dd1b55a
chore: merge main
JooKangsan 99ccb36
중복된 숫자 제거 / 기초
JooKangsan b2a8608
한 번만 등장한 문자 / 기초
JooKangsan ba19b5f
무작위로 K개의 수 뽑기 / 기초
JooKangsan 9c370e1
Contains Duplicate / 기초
JooKangsan 0e9b086
소인수분해 / 기초
JooKangsan e8548e0
Longest Consecutive Sequence / 중급
JooKangsan e0b9ef8
Repeated_DNA_Sequences / 중급
JooKangsan b0cb376
Set.md 파일 추가
JooKangsan 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,11 @@ | ||
| function solution(s) { | ||
| return s | ||
| .split('') | ||
| .sort() | ||
| .filter((char, idx, arr) => | ||
| char !== arr[idx-1] && char !== arr[idx+1] | ||
| ) | ||
| .join(''); | ||
| } | ||
| // 시간 복잡도 o(n log n) | ||
| // 공간 복잡도 o(n) |
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,8 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {boolean} | ||
| */ | ||
| var containsDuplicate = function (nums) { | ||
| const uniqueNums = [...new Set(nums)]; | ||
| return uniqueNums.length != nums.length; | ||
| }; |
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,20 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number} | ||
| */ | ||
| var longestConsecutive = function (nums) { | ||
| const numSet = new Set(nums); | ||
| let longestStreak = 0; | ||
| for (const num of numSet) { | ||
| if (!numSet.has(num - 1)) { | ||
| let currentNum = num; | ||
| let currentStreak = 1; | ||
| while (numSet.has(currentNum + 1)) { | ||
| currentNum += 1; | ||
| currentStreak += 1; | ||
| } | ||
| longestStreak = Math.max(longestStreak, currentStreak); | ||
| } | ||
| } | ||
| return longestStreak; | ||
| }; |
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,20 @@ | ||
| function solution(n) { | ||
| let primeFactors = new Set(); | ||
| let num = n; | ||
|
|
||
| while(num % 2 === 0) { | ||
| primeFactors.add(2); | ||
| num = num / 2; | ||
| } | ||
| for(let i = 3; i <= num; i += 2) { | ||
| while(num % i === 0) { | ||
| primeFactors.add(i); | ||
| num = num / i; | ||
| } | ||
| } | ||
| if(num > 2) { | ||
| primeFactors.add(num); | ||
| } | ||
|
|
||
| return [...primeFactors].sort((a, b) => a - b); | ||
| } | ||
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,4 @@ | ||
| function solution(arr, k) { | ||
| var answer = [...new Set(arr)]; | ||
| return [...answer.slice(0, k), ...Array(Math.max(0, k - answer.length)).fill(-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,4 @@ | ||
| function solution(my_string) { | ||
| var answer = [...new Set(my_string)].join(''); | ||
| return answer; | ||
| } |
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,18 @@ | ||
| /** | ||
| * @param {string} s | ||
| * @return {string[]} | ||
| */ | ||
| function findRepeatedDnaSequences(s) { | ||
| const see = new Set(); | ||
| const seen = new Set(); | ||
|
|
||
| for (let i = 0; i <= s.length - 10; i++) { | ||
| const sequence = s.slice(i, i + 10); | ||
| if (see.has(sequence)) { | ||
| seen.add(sequence); | ||
| } | ||
| see.add(sequence); | ||
| } | ||
|
|
||
| return [...seen]; | ||
| } |
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,141 @@ | ||
| ## 1. Set(집합) | ||
| - 중복을 허용하지 않는 값들의 모음을 표현하는 자료구조, | ||
| - 수학의 집합 개념을 구현한 자료구조 | ||
|
|
||
| ## 2. Set 구현 | ||
|
|
||
| ### 2.1 기본 생성 | ||
| ```javascript | ||
| // Set 생성 | ||
| const set = new Set(); | ||
| const setFromArray = new Set([1, 2, 3]); | ||
|
|
||
| // 값 추가/삭제 | ||
| set.add(4); // 값 추가 | ||
| set.delete(4); // 값 삭제 | ||
| set.clear(); // 모든 값 삭제 | ||
|
|
||
| // 값 확인 | ||
| set.has(4); // 값 존재 여부 | ||
| set.size; // Set 크기 | ||
| ``` | ||
|
|
||
| ### 2.2 기본 연산 | ||
| ```javascript | ||
| class CustomSet { | ||
| constructor() { | ||
| this.items = {}; | ||
| } | ||
|
|
||
| // 원소 추가 | ||
| add(element) { | ||
| if (!this.has(element)) { | ||
| this.items[element] = element; | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // 원소 삭제 | ||
| delete(element) { | ||
| if (this.has(element)) { | ||
| delete this.items[element]; | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // 원소 확인 | ||
| has(element) { | ||
| return element in this.items; | ||
| } | ||
|
|
||
| // 모든 원소 반환 | ||
| values() { | ||
| return Object.values(this.items); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## 3. Set 연산 메서드 | ||
|
|
||
| ### 3.1 합집합(Union) | ||
| ```javascript | ||
| function union(setA, setB) { | ||
| const unionSet = new Set(setA); | ||
| for (const element of setB) { | ||
| unionSet.add(element); | ||
| } | ||
| return unionSet; | ||
| } | ||
| ``` | ||
|
|
||
| ### 3.2 교집합(Intersection) | ||
| ```javascript | ||
| function intersection(setA, setB) { | ||
| const intersectionSet = new Set(); | ||
| for (const element of setA) { | ||
| if (setB.has(element)) { | ||
| intersectionSet.add(element); | ||
| } | ||
| } | ||
| return intersectionSet; | ||
| } | ||
| ``` | ||
|
|
||
| ### 3.3 차집합(Difference) | ||
| ```javascript | ||
| function difference(setA, setB) { | ||
| const differenceSet = new Set(setA); | ||
| for (const element of setB) { | ||
| differenceSet.delete(element); | ||
| } | ||
| return differenceSet; | ||
| } | ||
| ``` | ||
|
|
||
| ### 3.4 부분집합(Subset) 확인 | ||
| ```javascript | ||
| function isSubset(setA, setB) { | ||
| for (const element of setA) { | ||
| if (!setB.has(element)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| ``` | ||
|
|
||
| ## 4. Set 활용 | ||
|
|
||
| ### 4.1 배열 중복 제거 | ||
| ```javascript | ||
| function removeDuplicates(array) { | ||
| return [...new Set(array)]; | ||
| } | ||
|
|
||
| // 예시 | ||
| const array = [1, 2, 2, 3, 3, 4]; | ||
| console.log(removeDuplicates(array)); // [1, 2, 3, 4] | ||
| ``` | ||
|
|
||
| ### 4.2 문자열 중복 문자 제거 | ||
| ```javascript | ||
| function removeDuplicateChars(str) { | ||
| return [...new Set(str)].join(''); | ||
| } | ||
|
|
||
| // 예시 | ||
| console.log(removeDuplicateChars('hello')); // 'helo' | ||
| ``` | ||
|
|
||
| ## 5. 성능 고려사항 | ||
|
|
||
| ### 5.1 시간 복잡도 | ||
| - 추가(add): O(1) | ||
| - 삭제(delete): O(1) | ||
| - 검색(has): O(1) | ||
| - 크기 확인(size): O(1) | ||
|
|
||
| ### 5.2 공간 복잡도 | ||
| - O(n), n은 저장된 원소의 수 |
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.
코드 잘 보았습니다!
현재 i<=num까지 반복하는 방식보다,
모든 합성수 n은 적어도 하나의 소인수를 가지며, 그 소인수는 √n 이하특성을 이용해서 √n까지만 검사하면 더 효율적입니다!그리고 2를 특별히 처리한 후 홀수만 검사하는 아이디어는 좋지만, 하나의 루프로 통합하면 간결하게 표현할 수 있을 것 같습니다.
이 방식으로 하면 sort도 필요없어집니다!