Merged
Conversation
bona1122
approved these changes
Feb 19, 2025
Collaborator
bona1122
left a comment
There was a problem hiding this comment.
전체적으로 set을 잘 활용하신 것 같습니다. 많이 배우고 갑니다! 한주동안 수고하셨습니다!
bona1122
reviewed
Feb 19, 2025
Comment on lines
+1
to
+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); | ||
| } No newline at end of file |
Collaborator
There was a problem hiding this comment.
코드 잘 보았습니다!
현재 i<=num까지 반복하는 방식보다, 모든 합성수 n은 적어도 하나의 소인수를 가지며, 그 소인수는 √n 이하특성을 이용해서 √n까지만 검사하면 더 효율적입니다!
그리고 2를 특별히 처리한 후 홀수만 검사하는 아이디어는 좋지만, 하나의 루프로 통합하면 간결하게 표현할 수 있을 것 같습니다.
function solution(n) {
const factors = new Set()
for (let i = 2; i * i <= n; i++) {
while (n % i === 0) { // 동일한 소인수로 여러번 나누기 가능
factors.add(i)
n /= i
}
}
// 마지막으로 남은 수가 1보다 크면 그 자체가 소수이므로 추가
if (n > 1) factors.add(n)
return Array.from(factors)
}이 방식으로 하면 sort도 필요없어집니다!
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
집합
md 파일로 추가했습니다!
📌 푼 문제
📝 간단한 풀이 과정
중복된 문자 제거
한 번만 등장한 문자
무작위로 K개의 수 뽑기
Contains Duplicate
소인수분해
Longest Consecutive Sequence
Repeated DNA Sequences