You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
슬라이딩 윈도우 알고리즘을 사용하여 시간 복잡도를 O(n)으로 최적화했습니다.
Map을 활용하여 제품 수량 추적의 공간 복잡도도 효율적입니다.
functionsolution(want,number,discount){// 원하는 제품 정보 Map 초기화constwantMap=newMap();for(letidx=0;idx<want.length;idx++){wantMap.set(want[idx],number[idx]);}// 슬라이딩 윈도우에 필요한 변수들 정의letanswer=0;// 첫번째 윈도우 처리for(letday=0;day<=9;day++){constproduct=discount[day];if(wantMap.has(product)){wantMap.set(product,wantMap.get(product)-1);}}if([...wantMap.values()].every((num)=>num<=0)){answer+=1;}// 슬라이딩 윈도우for(letidx=0;idx<discount.length-10;idx++){// 앞의 것 빼기constfirst=discount[idx];if(wantMap.has(first)){wantMap.set(first,wantMap.get(first)+1);}// 뒤의 것 추가하기constlast=discount[idx+10];if(wantMap.has(last)){wantMap.set(last,wantMap.get(last)-1);}// 가입 가능 여부 확인if([...wantMap.values()].every((num)=>num<=0)){answer++;}}returnanswer;}
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()와 같은 함수형 프로그래밍 메서드를 활용하여 코드를 더 간결하고 읽기 쉽게 만들 수 있습니다.
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.
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
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.
User description
오늘도 멋져요 👍✨
PR Type
Enhancement
Description
프로그래머스 두 문제의 새로운 솔루션 구현
슬라이딩 윈도우와 Map 활용한 효율적인 알고리즘
각 문제에 대한 2025.12.07 풀이 추가