[Coding Test]햄버거 만들기

이묘·2022년 12월 23일
0

CodingTest

목록 보기
36/41
post-thumbnail
function solution(ingredient) {
  var answer = 0;

  // 이것도 시도는 해봤는데 replace의 시간복잡도 때문에 타임아웃 뜸
  // test = ingredient.join("")
  // while(test.indexOf('1231') !== -1) {
  //   test = test.replace('1231', '')
  //   answer++;
  // }

  const array = [];

  ingredient.forEach(element => {
    array.push(element);

    if(array[array.length - 1] === 1 && array[array.length - 2] === 3
      && array[array.length - 3] === 2 && array[array.length - 4] === 1){
        array.pop();
        array.pop();
        array.pop();
        array.pop();
        answer++;
    }
  })

  return answer;
}
profile
본질을 공부해야 응용도 하지 않을까

0개의 댓글