Skip to content

Conversation

@uyeon0
Copy link
Collaborator

@uyeon0 uyeon0 commented Dec 4, 2025

User description

오늘도 멋져요 👍✨


PR Type

Enhancement


Description

  • 프로그래머스 3개 문제의 새로운 솔루션 추가

  • 각 문제에 대해 새로운 solution2 함수 구현

  • 기존 솔루션을 solution1로 리네임


@uyeon0 uyeon0 added the programmers Programmers 문제 풀이 label Dec 4, 2025
@github-actions
Copy link

github-actions bot commented Dec 4, 2025

PR Reviewer Guide 🔍

🧪 No relevant tests
⚡ Recommended focus areas for review

Algorithmic Optimization

새로운 솔루션은 기존 솔루션과 유사한 접근 방식을 사용하고 있습니다. 시간 복잡도는 O(n)으로 동일하지만, 변수 이름과 로직을 약간 개선할 수 있습니다.

function solution2(cards1, cards2, goal) {
  let front1 = 0;
  let front2 = 0;
  let len1 = cards1.length;
  let len2 = cards2.length;

  for (let idx = 0; idx < goal.length; idx++) {
    const target = goal[idx];
    if (front1 < len1 && cards1[front1] === target) front1++;
    else if (front2 < len2 && cards2[front2] === target) front2++;
    else return "No";
  }

  return "Yes";
}
Performance Concern

현재 구현은 중첩 루프를 사용하여 비효율적입니다. 단일 패스로 최적화할 수 있으며, 불필요한 반복을 줄일 수 있습니다.

function solution2(progresses, speeds) {
  let answer = [];
  let currentDay = 1;
  let deployCount = 0;

  while (deployCount < progresses.length) {
    let completedCount = deployCount;
    // 작업
    while (completedCount < progresses.length) {
      if (
        progresses[completedCount] + speeds[completedCount] * currentDay <
        100
      )
        break;
      completedCount++;
    }
    // 배포
    if (deployCount !== completedCount) {
      answer.push(completedCount - deployCount);
      deployCount = completedCount;
    }
    currentDay++;
  }

  return answer;
}
Code Simplification

Map 초기화 로직을 더 간결하게 만들 수 있습니다. reduce() 또는 Object.groupBy() 같은 메서드를 고려해보세요.

function solution2(participant, completion) {
  // 참여자 맵 초기화
  const participantMap = new Map(); // key: 이름, value: 해당하는 선수 수
  for (const name of participant) {
    if (participantMap.has(name)) {
      participantMap.set(name, participantMap.get(name) + 1);
    } else {
      participantMap.set(name, 1);
    }
  }

  // 완주자 확인
  for (const name of completion) {
    participantMap.set(name, participantMap.get(name) - 1);
  }

  // 완주하지 못한 선수 학인
  for (const [name, count] of participantMap) {
    if (count > 0) return name;
  }

  return ""; // NEVER
}

@github-actions
Copy link

github-actions bot commented Dec 4, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
배포 로직 단순화 및 가독성 개선

현재 코드는 중첩된 반복문으로 인해 복잡성이 높습니다. 배포 로직을 더 명확하고 간결하게 리팩토링할 수 있습니다.
배열 메서드와 수학적 접근을 활용하여 코드를 개선할 수 있습니다.

Programmers/Level2/42586_기능개발.js [9-34]

 function solution2(progresses, speeds) {
-  let answer = [];
-  let currentDay = 1;
-  let deployCount = 0;
+  const days = progresses.map((progress, i) => 
+    Math.ceil((100 - progress) / speeds[i])
+  );
 
-  while (deployCount < progresses.length) {
-    let completedCount = deployCount;
-    while (completedCount < progresses.length) {
-      if (
-        progresses[completedCount] + speeds[completedCount] * currentDay <
-        100
-      )
-        break;
-      completedCount++;
+  const answer = [];
+  let maxDay = days[0];
+  let count = 1;
+
+  for (let i = 1; i < days.length; i++) {
+    if (days[i] <= maxDay) {
+      count++;
+    } else {
+      answer.push(count);
+      maxDay = days[i];
+      count = 1;
     }
-    if (deployCount !== completedCount) {
-      answer.push(completedCount - deployCount);
-      deployCount = completedCount;
-    }
-    currentDay++;
   }
+  answer.push(count);
 
   return answer;
 }
Suggestion importance[1-10]: 8

__

Why: The suggestion significantly improves the code by replacing nested loops with a more mathematical and functional approach. It uses map() and a single pass to calculate deployment days, making the code more readable and potentially more efficient.

Medium
코드 간결성 및 가독성 개선

현재 코드는 불필요한 변수와 반복문 로직이 있습니다. 코드를 더 간결하고 가독성 있게 리팩토링할 수 있습니다.
불필요한 길이 변수를 제거하고, 조기 반환 패턴을 활용하여 코드를 단순화할 수 있습니다.

Programmers/Level1/159994_카드_뭉치.js [9-23]

 function solution2(cards1, cards2, goal) {
-  let front1 = 0;
-  let front2 = 0;
-  let len1 = cards1.length;
-  let len2 = cards2.length;
+  let front1 = 0, front2 = 0;
 
-  for (let idx = 0; idx < goal.length; idx++) {
-    const target = goal[idx];
-    if (front1 < len1 && cards1[front1] === target) front1++;
-    else if (front2 < len2 && cards2[front2] === target) front2++;
+  for (const target of goal) {
+    if (front1 < cards1.length && cards1[front1] === target) front1++;
+    else if (front2 < cards2.length && cards2[front2] === target) front2++;
     else return "No";
   }
 
   return "Yes";
 }
Suggestion importance[1-10]: 7

__

Why: The suggestion simplifies the code by removing unnecessary length variables and using a more concise for...of loop. The logic remains functionally equivalent.

Medium
Map 초기화 로직 간소화

Map 초기화 로직을 더 간결하게 만들 수 있습니다. getOrDefault 패턴을 사용하여 코드를 줄이고 가독성을 높일 수 있습니다.
또한 빈 문자열
반환은 불필요하므로 제거할 수 있습니다.

Programmers/Level1/42576_완주하지_못한_선수.js [9-31]

 function solution2(participant, completion) {
   const participantMap = new Map();
   for (const name of participant) {
-    if (participantMap.has(name)) {
-      participantMap.set(name, participantMap.get(name) + 1);
-    } else {
-      participantMap.set(name, 1);
-    }
+    participantMap.set(name, (participantMap.get(name) || 0) + 1);
   }
 
   for (const name of completion) {
     participantMap.set(name, participantMap.get(name) - 1);
   }
 
   for (const [name, count] of participantMap) {
     if (count > 0) return name;
   }
-
-  return "";
 }
Suggestion importance[1-10]: 6

__

Why: The suggestion uses a more concise way to initialize the Map with the (participantMap.get(name) || 0) + 1 pattern. The code becomes slightly more readable.

Low

@yoouyeon yoouyeon added the ready-to-merge pr을 머지해주세요 label Dec 4, 2025
@uyeon0 uyeon0 merged commit ee2774d into main Dec 4, 2025
8 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