[프로그래머스 Lv1] 크레인 인형뽑기 게임

수민이슈·2023년 4월 26일
0

[C++] 코딩테스트

목록 보기
26/116
post-thumbnail

🖊️ 문제

https://school.programmers.co.kr/learn/courses/30/lessons/64061

🖊️ 풀이

음 ~~ 스택이군
쉽게 구현했으나
원소 접근하는게 살짝 헷갈렷다면 실화가..?

🖊️ 코드

#include <string>
#include <vector>
#include <iostream>
#include <stack>

using namespace std;

int solution(vector<vector<int>> board, vector<int> moves) {
    int answer = 0;
    stack<int> buckets;

    for (auto& m : moves) {
        for (int j = 0 ; j < board.size() ; j++ ) {
            int doll = board[j][m-1];
            if (doll == 0) continue;
            else {
                board[j][m-1] = 0;
                if (buckets.empty()) buckets.push(doll);
                else if (buckets.top() != doll) buckets.push(doll);
                else {
                    buckets.pop();
                    answer += 2;
                }
                break;
            }
        }  
    }
    return answer;
}

0개의 댓글