# 생각

stack을 이용하여 top과 넣을 인형이 같다면 pop해주고 개수를 늘려주면 된다

뽑은 인형은 0으로 초기화를 해준다

 

 

# 전체 코드

#include <bits/stdc++.h>
using namespace std;

int solution(vector<vector<int>> board, vector<int> moves)
{
	int answer = 0;
	stack<int> box;
	for (int i = 0; i < moves.size(); i++)
	{
		for (int j = 0; j < moves.size(); j++)
		{
			int col = moves[i] - 1;
			if (board[j][col] != 0)
			{
				if (!box.empty() and box.top() == board[j][col])
				{
					box.pop();
					answer += 2;
				}
				else
				{
					box.push(board[j][col]);
				}
				board[j][col] = 0;
				break;
			}
		}
	}
	return answer;
}

 


출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges

+ Recent posts