[프로그래머스] 햄버거 만들기(Kotlin)

0

프로그래머스

목록 보기
105/128
post-thumbnail

[프로그래머스] 햄버거 만들기(Kotlin)

풀이

import java.util.*

class Solution {
    fun solution(ingredient: IntArray): Int {
        //1: 빵, 2: 야채, 3: 고기, 4: 빵
        val st = Stack<Int>()
    
        var answer: Int = 0
        for(ing in ingredient){
            st.push(ing)
            
            //빵 야채 고기 빵 확인
            if(st.size >= 4){
                val peek1 = st.peek()
                st.pop()
                val peek2 = st.peek()
                st.pop()
                val peek3 = st.peek()
                st.pop()
                val peek4 = st.peek()
                st.pop()
                
                if((peek4 == 1) && (peek3 == 2) && (peek2 == 3) && (peek1 == 1)) answer++;
                else{
                    //원상복구
                    st.push(peek4)
                    st.push(peek3)
                    st.push(peek2)
                    st.push(peek1)
                }
            }
        }
        return answer
    }
}
profile
Be able to be vulnerable, in search of truth

0개의 댓글