Skip to content

Conversation

@uyeon0
Copy link
Collaborator

@uyeon0 uyeon0 commented Jan 23, 2026

User description

오늘도 멋져요 👍✨


PR Type

Enhancement


Description

  • 프로그래머스 레벨 1 문제 두 개 해결

    • 에라토스테네스의 체 알고리즘 사용
  • README.md 문제 목록 업데이트


@uyeon0 uyeon0 added the programmers Programmers 문제 풀이 label Jan 23, 2026
@github-actions
Copy link

PR Reviewer Guide 🔍

🧪 No relevant tests
⚡ Recommended focus areas for review

성능 최적화

에라토스테네스의 체 알고리즘의 시간 복잡도를 O(n log log n)으로 최적화했지만, 추가 개선 가능성 검토 필요

function countPrime(n) {
  const isPrime = Array.from({length: n + 1}).fill(true);
  isPrime[0] = false;
  isPrime[1] = false;

  for (let num = 2; num * num <= n; num++) {
      if (isPrime[num] === false) continue;
      for (let k = num * num; k <= n; k += num) {
          isPrime[k] = false;
      }
  }

  return isPrime.filter((value) => value).length;
}
알고리즘 효율성

삼중 for문 사용으로 시간 복잡도가 O(n³)이므로, 더 효율적인 접근 방법 고려 필요

function solution(nums) {
  let answer = 0;
  const isPrime = eratosthenes(2997); // 주어진 숫자 3개를 더했을 때 나올 수 있는 최댓값이 2997 (1000+999+998)

  for (let a = 0; a < nums.length - 2; a++) {
    for (let b = a + 1; b < nums.length - 1; b++) {
      for (let c = b + 1; c < nums.length; c++) {
        if (isPrime[nums[a] + nums[b] + nums[c]]) answer++;
      }
    }
  }

  return answer;
}

@github-actions
Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
소수 판별 및 조합 계산 최적화

소수 판별 로직을 분리하고, 최대 합계를 동적으로 계산하여 메모리 사용을 최적화할 수 있습니다. 하드코딩된 최대값 대신 입력 배열의 최대값을
사용하세요.

Programmers/Level1/12977_소수_만들기.js [14-41]

 function eratosthenes(n) {
-  const isPrime = Array.from({ length: n + 1 }).fill(true);
-  isPrime[0] = false;
-  isPrime[1] = false;
+  const isPrime = new Array(n + 1).fill(true);
+  isPrime[0] = isPrime[1] = false;
+  
   for (let num = 2; num * num <= n; num++) {
-    if (isPrime[num] === false) continue;
+    if (!isPrime[num]) continue;
     for (let k = num * num; k <= n; k += num) {
       isPrime[k] = false;
     }
   }
 
   return isPrime;
 }
 
 function solution(nums) {
-  let answer = 0;
-  const isPrime = eratosthenes(2997);
+  const maxSum = nums.reduce((a, b) => a + b, 0);
+  const isPrime = eratosthenes(maxSum);
 
-  for (let a = 0; a < nums.length - 2; a++) {
-    for (let b = a + 1; b < nums.length - 1; b++) {
+  return nums.reduce((count, _, a) => {
+    for (let b = a + 1; b < nums.length; b++) {
       for (let c = b + 1; c < nums.length; c++) {
-        if (isPrime[nums[a] + nums[b] + nums[c]]) answer++;
+        if (isPrime[nums[a] + nums[b] + nums[c]]) count++;
       }
     }
-  }
-
-  return answer;
+    return count;
+  }, 0);
 }
Suggestion importance[1-10]: 8

__

Why: The suggestion significantly improves the solution by dynamically calculating the maximum sum and using a more functional approach with reduce(). It removes the hardcoded maximum value and makes the code more flexible and memory-efficient.

Medium
소수 계산 알고리즘 최적화

에라토스테네스의 체 구현을 더 명확하고 효율적으로 개선할 수 있습니다. 불필요한 반복을 줄이고 가독성을 높일 수 있습니다.

Programmers/Level1/12921_소수_찾기.js [13-26]

 function countPrime(n) {
-  const isPrime = Array.from({length: n + 1}).fill(true);
-  isPrime[0] = false;
-  isPrime[1] = false;
+  const isPrime = new Array(n + 1).fill(true);
+  isPrime[0] = isPrime[1] = false;
   
   for (let num = 2; num * num <= n; num++) {
-      if (isPrime[num] === false) continue;
+      if (!isPrime[num]) continue;
       for (let k = num * num; k <= n; k += num) {
           isPrime[k] = false;
       }
   }
   
-  return isPrime.filter((value) => value).length;
+  return isPrime.filter(Boolean).length;
 }
Suggestion importance[1-10]: 7

__

Why: The suggestion provides minor improvements to the countPrime function, such as using new Array() instead of Array.from() and simplifying boolean checks. The core algorithm remains the same, offering slight readability and performance enhancements.

Medium

@yoouyeon yoouyeon self-assigned this Jan 23, 2026
@yoouyeon yoouyeon added the ready-to-merge pr을 머지해주세요 label Jan 23, 2026
@uyeon0 uyeon0 merged commit ae10603 into main Jan 23, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

programmers Programmers 문제 풀이 ready-to-merge pr을 머지해주세요

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants