본문 바로가기
코딩테스트/프로그래머스

[프로그래머스 Lv.1 C++/C#] 햄버거 만들기

by 으얏 2023. 1. 11.

C++ 답안입니다.

#include <string>
#include <vector>

using namespace std;

int solution(vector<int> ingredient)
{
	int answer = 0;
	vector<int> burger = { -1 };
	for (int x : ingredient)
	{
		if (burger.back() == 1 && x == 2)
		{
			burger.back() = 12;
		}
		else if (burger.back() == 12 && x == 3)
		{
			burger.back() = 123;
		}
		else if (burger.back() == 123 && x == 1)
		{
			burger.pop_back();
			answer++;
		}
		else burger.push_back(x);
	}
	return answer;
}

 

C# 답안입니다.

using System;
using System.Collections.Generic;
public class Solution {
    public int solution(int[] ingredient) {
            int answer = 0;
           List<int> list = new List<int>();

            foreach (int burger in ingredient)
            {
                list.Add(burger);

                if (list.Count >= 4)
                {
                    if (list[list.Count - 4] == 1 && 
                        list[list.Count - 3] == 2 && 
                        list[list.Count - 2] == 3 && 
                        list[list.Count - 1] == 1)
                    {
                        answer++;
                        list.RemoveRange(list.Count - 4, 4);
                    }
                }
            }
        return answer;
    }
}

댓글