๐ ์๊ณ ๋ฆฌ์ฆ/Programmers
[Javascript/PGS] Lv.2 : ํ ์ธ ํ์ฌ (์ฌ๋ผ์ด๋ฉ ์๋์ฐ)
xxilliant
2025. 2. 6. 15:19
728x90
๋ฐ์ํ
https://school.programmers.co.kr/learn/courses/30/lessons/131127#
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก, ์ฑ์ฉ๊น์ง Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
(ํ์ด ์ฝ๋๋ ํฌ์คํ ๋งจ ์๋์)
ํ๋ก๊ทธ๋๋จธ์ค ๋ ๋ฒจ 2 ์ค, ์ฌ๋ผ์ด๋ฉ ์๋์ฐ ๋ฌธ์ ์ด๋ค.
๋ณดํต ๋ธ๋ฃจํธ ํฌ์ค(์ํ)์ผ๋ก ์๊ฐ์ด๊ณผ๊ฐ ๋๋ ๋ฌธ์ ๋ฅผ sliding window๋ก ํ๋ฉด ํด๊ฒฐ๋๋ ๊ฒฝ์ฐ๊ฐ ์๋ค.
์ด ๋ถ์ ์์์ด ํฐ ๋์์ด ๋์๋ค
left, right ์ผ์ผ์ด ๋ณ๊ฒฝํ ํ์ ์์ด, ๋ ๋จ์ํ๊ฒ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์ธ ์ ์์์!
https://youtu.be/ot5mnp_hTqo?si=v_OlfFmNMui0SHuF
๋์ ํ์ด
function solution(want, number, discount) {
let answer = 0;
let windowSize = 10;
let discountMap = new Map();
for(let i=0; i<number.length; ++i){
discountMap.set(want[i],Number(number[i]));
}
let right = 9;
for(let i=0; i<discount.length; ++i){
if(discountMap.has(discount[i])) discountMap.set(discount[i],discountMap.get(discount[i])-1);
if(i>=right){
// ๋น๊ต
if([...discountMap.values()].map(d => d<=0).every(Boolean)) {
// console.log(discountMap)
answer++;
}
// ์ฌ๋ผ์ด๋ฉ
if(discountMap.has(discount[i-9])) discountMap.set(discount[i-9],discountMap.get(discount[i-9])+1);
}
}
return answer;
}
728x90
๋ฐ์ํ