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
๋ฐ์ํ
'๐ ์๊ณ ๋ฆฌ์ฆ > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Javascript/PGS] Lv.0 : ์นํจ ์ฟ ํฐ (0) | 2025.02.11 |
---|---|
[Javascript/PGS] Lv.3 : ์ ๊ตญ์ฌ์ฌ (์ด๋ถํ์) (1) | 2025.02.06 |
[C++/PGS] Lv.0 : ๋ฑ์ ๋งค๊ธฐ๊ธฐ (0) | 2025.01.13 |
[C++/PGS] Lv.3 : ์ต๊ณ ์ ์งํฉ (๋ฒกํฐ, ์ํ) (0) | 2025.01.07 |
[C++/PGS] Lv.3 : ์ซ์ ๊ฒ์ (๊ทธ๋ฆฌ๋ Greedy) (0) | 2025.01.07 |