[프로그래머스 Level 2] 할인 행사
1분 읽기
풀이 1: 투 포인터
function solution(want, number, discount) {
let answer = 0;
let start = 0;
let end = 9;
let products = Object.fromEntries(want.map((product, i) => [product, number[i]]));
let remain = new Set(want);
const put = (product) => {
products[product] -= 1;
if(products[product] <= 0) remain.delete(product);
}
const out = (product) => {
products[product] += 1;
if(products[product] > 0) remain.add(product);
}
for(let i = start; i <= end; i++){
put(discount[i])
}
while(end < discount.length){
if(remain.size === 0){
answer++;
}
out(discount[start++]);
put(discount[++end]);
}
return answer;
}Two Pointers7편 중 2번째
관련 글
4분 읽기
소수 구하기
소수 판별법과 소수를 구하는 알고리즘을 정리합니다.
1분 읽기
[프로그래머스 Level 2] 미로 탈출
프로그래머스 미로 탈출을 BFS로 풀이합니다.
1분 읽기
[프로그래머스 Level 2] 마법의 엘리베이터
프로그래머스 마법의 엘리베이터를 자리수 그리디로 풀이합니다.