https://programmers.co.kr/learn/courses/30/lessons/64061
코딩테스트 연습 - 크레인 인형뽑기 게임
[[0,0,0,0,0],[0,0,1,0,3],[0,2,5,0,1],[4,2,4,4,2],[3,5,1,3,1]] [1,5,3,5,1,2,1,4] 4
programmers.co.kr
stack을이용하면 간단
인형을 뽑을 때마다 해당 인형의 값을 stack에 push
push하기 전에 조건문으로 stack.top()의 값이 지금 뽑은 인형과 같다면 pop()으로 터트리고 answer 2 증가(인형 두개가 터지니까)
뽑은 인형의 자리는 빈 자리 (0)으로 만듬
int solution(vector<vector<int>> board, vector<int> moves) {
int answer = 0;
stack<int> stack;
for (int i = 0; i < moves.size(); i++) {
for (int j = 0;j < board.size();j++) {
if (board[j][moves[i]-1] != 0) {
if (!stack.empty() && board[j][moves[i] - 1] == stack.top()) {
stack.pop();
answer+=2;
}
else
stack.push(board[j][moves[i] - 1]);
board[j][moves[i] - 1] = 0;
break;
}
}
}
return answer;
}
'코딩테스트 > 문제풀이' 카테고리의 다른 글
[프로그래머스] 타겟넘버 C++ (dfs) (0) | 2022.05.05 |
---|---|
[프로그래머스] 카카오프렌즈 컬러링 북 C++ (dfs) (0) | 2022.05.03 |
[프로그래머스] 키패드 누르기 C++ (0) | 2022.05.03 |
[프로그래머스] 숫자 문자열과 영단어 C++ (0) | 2022.05.02 |
[프로그래머스] [1차] 추석 트래픽 C++ (0) | 2022.05.02 |