(수근수근)
크레인 인형뽑기 게임 본문
[문제]
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
[해결]
인형이 있는 board는 queue배열로 만들어서
moves에 index로 바로 가장 위에 있는 인형을 뽑는다.
바구니는 stack을 사용하여 인형 넣고, 빼는 순서의 고려를 줄인다.
[코드]
class kakaoSolution {
public int solution(int[][] board, int[] moves) {
int answer = 0;
int size = board[0].length;
Queue<Integer>[] queue = new Queue[size];
for (int i = 0; i < size; i++) {
queue[i] = new LinkedList<Integer>();
}
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (board[i][j] == 0)
continue;
queue[j].offer(board[i][j]);
}
}
// 바구니 만들기
Stack<Integer> bucket = new Stack<Integer>();
int num;
// 바구니에 있는 거 뺴기
for (int i = 0; i < moves.length; i++) {
if (queue[moves[i] - 1].size()==0) continue;
num = queue[moves[i]-1].poll();
if (i == 0) {
bucket.push(num);
continue;
}
if(!bucket.isEmpty()) {
int bucketTopnum = bucket.peek();
if(num == bucketTopnum) {
bucket.pop();
answer= answer+2;
continue;
}
}
bucket.push(num);
}
return answer;
}
}
'Algorithm' 카테고리의 다른 글
SQL 정리 (0) | 2020.05.27 |
---|---|
[백준] 단어공부 (0) | 2020.05.26 |
[프로그래머스] SQL문제풀기 & 정리 (0) | 2020.05.25 |
[백준] 2667 단지번호붙이기 (0) | 2020.04.20 |
[백준] DFS와 BFS (0) | 2020.04.06 |
Comments