133502
Link
https://school.programmers.co.kr/learn/courses/30/lessons/133502 (opens in a new tab)
Answer
JavaScript
function solution(ingredient) {
let stk = [];
let count = 0;
for (let i = 0; i < ingredient.length; i++) {
stk.push(ingredient[i]);
if (
stk[stk.length - 1] === 1 &&
stk[stk.length - 2] === 3 &&
stk[stk.length - 3] === 2 &&
stk[stk.length - 4] === 1
) {
count++;
stk.splice(-4);
}
}
return count;
}
function solution(ingredient) {
let count = 0;
for (let i = 0; i < ingredient.length; i++) {
if (ingredient.slice(i, i + 4).join("") === "1231") {
count++;
ingredient.splice(i, 4);
i -= 3;
}
}
return count;
}