728x90
https://school.programmers.co.kr/learn/courses/30/lessons/133502
처음 시도 코드
function solution(ingredient) {
var answer = 0;
let stringIngredient = ingredient.join("")
let burger = 1231
while(stringIngredient.includes(burger)){
stringIngredient = stringIngredient.replace(burger, "")
answer++
}
return answer;
}
문제점: replace를 사용하니 시간 복잡도가 높아져서 시간초과가 발생
해결 방안: stack 활용
function solution(ingredient) {
var answer = 0;
let stack = []
ingredient.forEach((item)=>{
stack.push(item)
let stackLength = stack.length
if(stack[stackLength-4] === 1 &&
stack[stackLength-3] === 2 &&
stack[stackLength-2] === 3 &&
stack[stackLength-1] === 1){
for(let i=0; i<4; i++){
stack.pop()
}
answer++
}
})
return answer;
}
728x90
'[항해99] TIL' 카테고리의 다른 글
29일차 (git 충돌...) (0) | 2023.09.19 |
---|---|
6주차 WIL (Axios) (0) | 2023.09.18 |
27일차 (react-router-dom :outlet) (0) | 2023.09.14 |
26일차 (react-Cookie) (0) | 2023.09.12 |
25일차 (useQueries) (0) | 2023.09.11 |