Skip to content

Conversation

@uyeon0
Copy link
Collaborator

@uyeon0 uyeon0 commented Dec 7, 2025

User description

오늘도 멋져요 👍✨


PR Type

Enhancement


Description

  • 프로그래머스 두 문제의 새로운 솔루션 구현

  • 슬라이딩 윈도우와 Map 활용한 효율적인 알고리즘

  • 각 문제에 대한 2025.12.07 풀이 추가


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

github-actions bot commented Dec 7, 2025

PR Reviewer Guide 🔍

🧪 No relevant tests
⚡ Recommended focus areas for review

Algorithmic Efficiency

슬라이딩 윈도우 알고리즘을 사용하여 시간 복잡도를 O(n)으로 최적화했습니다.
Map을 활용하여 제품 수량 추적의 공간 복잡도도 효율적입니다.

function solution(want, number, discount) {
  // 원하는 제품 정보 Map 초기화
  const wantMap = new Map();
  for (let idx = 0; idx < want.length; idx++) {
    wantMap.set(want[idx], number[idx]);
  }

  // 슬라이딩 윈도우에 필요한 변수들 정의
  let answer = 0;

  // 첫번째 윈도우 처리
  for (let day = 0; day <= 9; day++) {
    const product = discount[day];
    if (wantMap.has(product)) {
      wantMap.set(product, wantMap.get(product) - 1);
    }
  }

  if ([...wantMap.values()].every((num) => num <= 0)) {
    answer += 1;
  }

  // 슬라이딩 윈도우
  for (let idx = 0; idx < discount.length - 10; idx++) {
    // 앞의 것 빼기
    const first = discount[idx];
    if (wantMap.has(first)) {
      wantMap.set(first, wantMap.get(first) + 1);
    }

    // 뒤의 것 추가하기
    const last = discount[idx + 10];
    if (wantMap.has(last)) {
      wantMap.set(last, wantMap.get(last) - 1);
    }

    // 가입 가능 여부 확인
    if ([...wantMap.values()].every((num) => num <= 0)) {
      answer++;
    }
  }

  return answer;
}
Data Structure Optimization

Map을 사용하여 사용자 닉네임 관리의 시간 복잡도를 O(1)로 최적화했습니다.
두 번의 순회로 최종 닉네임과 메시지를 효율적으로 처리합니다.

function solution(record) {
  const userMap = new Map();
  for (let idx = 0; idx < record.length; idx++) {
    const [type, uid, name] = record[idx].split(" ");
    if (type === "Enter" || type === "Change") {
      userMap.set(uid, name);
    }
  }

  const answer = [];
  for (let idx = 0; idx < record.length; idx++) {
    const [type, uid, _] = record[idx].split(" ");
    if (type === "Enter") answer.push(`${userMap.get(uid)}님이 들어왔습니다.`);
    else if (type === "Leave")
      answer.push(`${userMap.get(uid)}님이 나갔습니다.`);
  }

  return answer;
}

@github-actions
Copy link

github-actions bot commented Dec 7, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
슬라이딩 윈도우 로직 단순화

코드의 중복된 로직을 리팩토링하여 가독성과 유지보수성을 개선할 수 있습니다. 첫 번째 윈도우 처리와 슬라이딩 윈도우 로직을 통합하고, 중복 코드를
제거할 수 있습니다.

Programmers/Level2/131127_할인_행사.js [8-52]

 function solution(want, number, discount) {
-  // 원하는 제품 정보 Map 초기화
-  const wantMap = new Map();
-  for (let idx = 0; idx < want.length; idx++) {
-    wantMap.set(want[idx], number[idx]);
-  }
-
-  // 슬라이딩 윈도우에 필요한 변수들 정의
+  const wantMap = new Map(want.map((item, idx) => [item, number[idx]]));
   let answer = 0;
 
-  // 첫번째 윈도우 처리
-  for (let day = 0; day <= 9; day++) {
-    const product = discount[day];
-    if (wantMap.has(product)) {
-      wantMap.set(product, wantMap.get(product) - 1);
-    }
-  }
+  for (let idx = 0; idx <= discount.length - 10; idx++) {
+    const windowMap = new Map(wantMap);
 
-  if ([...wantMap.values()].every((num) => num <= 0)) {
-    answer += 1;
-  }
-
-  // 슬라이딩 윈도우
-  for (let idx = 0; idx < discount.length - 10; idx++) {
-    // 앞의 것 빼기
-    const first = discount[idx];
-    if (wantMap.has(first)) {
-      wantMap.set(first, wantMap.get(first) + 1);
+    for (let day = 0; day < 10; day++) {
+      const product = discount[idx + day];
+      if (windowMap.has(product)) {
+        windowMap.set(product, windowMap.get(product) - 1);
+      }
     }
 
-    // 뒤의 것 추가하기
-    const last = discount[idx + 10];
-    if (wantMap.has(last)) {
-      wantMap.set(last, wantMap.get(last) - 1);
-    }
-
-    // 가입 가능 여부 확인
-    if ([...wantMap.values()].every((num) => num <= 0)) {
+    if ([...windowMap.values()].every((num) => num <= 0)) {
       answer++;
     }
   }
 
   return answer;
 }
Suggestion importance[1-10]: 8

__

Why: The suggestion significantly simplifies the sliding window logic by reducing code complexity and removing redundant iterations. It creates a more concise and readable implementation of the solution.

Medium
배열 순회 최적화 및 함수형 접근

배열 순회를 두 번 하는 대신 단일 순회로 최적화할 수 있습니다. 또한 map()filter()와 같은 함수형 프로그래밍 메서드를 활용하여 코드를
더 간결하고 읽기 쉽게 만들 수 있습니다.

Programmers/Level2/42888_오픈채팅방.js [8-27]

 function solution(record) {
   const userMap = new Map();
-  for (let idx = 0; idx < record.length; idx++) {
-    const [type, uid, name] = record[idx].split(" ");
+  
+  record.forEach(entry => {
+    const [type, uid, name] = entry.split(" ");
     if (type === "Enter" || type === "Change") {
       userMap.set(uid, name);
     }
-  }
+  });
 
-  const answer = [];
-  for (let idx = 0; idx < record.length; idx++) {
-    const [type, uid, _] = record[idx].split(" ");
-    if (type === "Enter") answer.push(`${userMap.get(uid)}님이 들어왔습니다.`);
-    else if (type === "Leave")
-      answer.push(`${userMap.get(uid)}님이 나갔습니다.`);
-  }
-
-  return answer;
+  return record
+    .filter(entry => entry.startsWith("Enter") || entry.startsWith("Leave"))
+    .map(entry => {
+      const [type, uid] = entry.split(" ");
+      const username = userMap.get(uid);
+      return type === "Enter" 
+        ? `${username}님이 들어왔습니다.`
+        : `${username}님이 나갔습니다.`;
+    });
 }
Suggestion importance[1-10]: 7

__

Why: The suggestion improves code readability by using functional programming methods like forEach(), filter(), and map(). It reduces the number of iterations and makes the code more declarative, though the performance impact might be minimal.

Medium

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