문제
나의 풀이 1
function solution(ing) {
var answer = 0;
let L = ing.length
let l=0
for(i=0; i<L; i+=l){
// for(i of ing){
let key = ing.slice(i,i+4).join("")
if(key == "1231"){
// console.log("hey!")
ing.splice(i,4)
answer += 1
// console.log(ing)
l= -2
}else{
l=1
}
}
return answer;
}
나의 풀이 수정후
function solution(ing) {
var answer = 0;
let L = ing.length
let l=0
for(i=0; i<L; i+=l){
// for(i of ing){
let key = ing.slice(i,i+4).join("")
if(key == "1231"){
// console.log("hey!")
ing.splice(i,4)
answer += 1
// console.log(ing)
if(i>1){
l= -2
}else {l=-1}
}else{
l=1
}
}
return answer;
}
넘의 풀이1
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;
}