lv1 문제입니다.
스텍을 이용합니다.
가로로 인형들이 주어진 board는 불편해서 세로로 배열을 하나 새로 만들었습니다.
#include <string>
#include <vector>
using namespace std;
int solution(vector<vector<int>> board, vector<int> moves) {
vector<vector<int>>mp(30);
while(!board.empty()){
vector<int>temp=board.back();
board.pop_back();
for(int i=0;i<temp.size();i++){
if(temp[i]==0)continue;
mp[i].push_back(temp[i]);
}
}
int answer = 0;
vector<int>st;
for(int i=0;i<moves.size();i++){
if(mp[moves[i]-1].empty())continue;
int top=mp[moves[i]-1].back();
mp[moves[i]-1].pop_back();
if(st.empty()){
st.push_back(top);
continue;
}
if(st.back()==top){
st.pop_back();
answer+=2;
continue;
}
st.push_back(top);
}
return answer;
}
'programmers' 카테고리의 다른 글
[Programmers]깊이/너비 우선 탐색(DFS/BFS)>단어 변환 (0) | 2022.08.26 |
---|---|
[Programmers] 깊이/너비 우선 탐색>>(DFS/BFS)게임 맵 최단거리 (0) | 2022.08.26 |
[Programmers]깊이/너비 우선 탐색(DFS/BFS)>타겟 넘버 (0) | 2022.08.25 |
2021 KAKAO BLIND RECRUITMENT > 신규 아이디 추천 + C++ String 정리 (0) | 2022.07.28 |
2022 KAKAO BLIND RECRUITMENT > 신고 결과 받기 + 중복제거하는법 (0) | 2022.07.28 |