프로그래머스 LEVEL 1(햄버거 만들기)
-
사용 언어 : javascript
-
해결 날짜 : 2022-11-21
- 해결 방법 :
- 인자로 입력받은 ingredient를 돌며 stack에 하나씩 추가
- stack의 길이가 4이상이 될 때,
- 스택의 마지막 4개의 인덱스 값이 햄버거를 만들 수 있는 [1, 2, 3, 1] 이라면 answer 증가 및 stack에서 4개 pop()
- 회고 :
- x
-
코드
function solution(ingredient) { let answer = 0; const stack = []; const BURGER = [1, 2, 3, 1]; for (const value of ingredient) { stack.push(value); if (stack.length >= 4) { const firstIndex = stack.length - 4; let canPave = true; for (const [idx, val] of BURGER.entries()) { if (val !== stack[firstIndex + idx]) { canPave = false; break; } } if (canPave) { answer++; stack.pop(); stack.pop(); stack.pop(); stack.pop(); } } } return answer; }
- 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges