Notice
Recent Posts
Recent Comments
Today
Total
01-27 06:30
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ ๊ฒŒ์ž„ ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ ๊ฒŒ์ž„

WonderJay 2022. 7. 1. 21:02
728x90
๋ฐ˜์‘ํ˜•
SMALL

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

move ๋ฐฐ์—ด์— ๋‹ด๊ธด ์ˆœ์„œ๋Œ€๋กœ ์ธํ˜•์„ stack ์— ๋‹ด๋Š”๋ฐ, ๋„ฃ์„ ์ธํ˜•๊ณผ stack ์˜ top ์ด ๊ฐ™๋‹ค๋ฉด ์ƒˆ๋กœ์šด ์ธํ˜•์„ push ํ•˜์ง€ ์•Š๊ณ  stack ์˜ top ์„ pop ํ•œ ๋’ค, answer ์„ 2 ์”ฉ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

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

using namespace std;

int solution(vector<vector<int>> board, vector<int> moves) {
    int answer = 0;
    
    stack<int> bucket;
    
    for (int i = 0; i < moves.size(); i++)
    {
        int  j = moves[i] - 1;
        int k = 0;

        for (int k = 0; k < board.size(); k++)
        {
            if (board[k][j] == 0)
                continue;
            else
            {
                if (!bucket.empty() && bucket.top() == board[k][j])
                {
                    bucket.pop();
                    answer += 2;
                    board[k][j] = 0;
                    break;
                }

                bucket.push(board[k][j]);
                board[k][j] = 0;

                break;
            }

        }
    }
    return answer;
}

 

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments