# 생각
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
'Algorithm > 프로그래머스' 카테고리의 다른 글
[Lv.2] 전화번호 목록 with cpp (0) | 2022.02.28 |
---|---|
[Lv.1] 완주하지 못한 선수 with cpp (0) | 2022.02.28 |
[Lv.1] 키패드 누르기 with cpp (0) | 2022.02.20 |
[Lv.1] 신규 아이디 추천 with cpp (0) | 2022.02.10 |
[Lv.1] 로또의 최고 순위와 최저 순위 with cpp (0) | 2022.02.09 |