본문 바로가기
Coding Test Java

인프런 - 크레인 인형 뽑기 (카카오)

by seonggu 2024. 1. 29.
import java.util.*;
class Main {	
	public int solution(int[][] board, int[] moves){
		int answer=0;
        Stack<Integer> stack = new Stack<>();
        
        for(int pos : moves){
            for(int i = 0; i < board.length; i++){ // 행크기 열은 board[0].length
                if(board[i][pos-1] != 0) {
                    int tmp = board[i][pos-1]; // index 0부터 i는 행 pos-1 열
                    board[i][pos-1] = 0;
                
                    if(!stack.isEmpty() && tmp == stack.peek()){ //스택마지막 값과 비교
                        answer += 2;
                        stack.pop();
                        
                    }else {
                        stack.push(tmp); 
                    }
                    break;
                    
                }
            }
        }
        
		return answer;
	}
	public static void main(String[] args){
		Main T = new Main();
		Scanner kb = new Scanner(System.in);
		int n=kb.nextInt();
		int[][] board=new int[n][n];
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				board[i][j]=kb.nextInt();
			}
		}
		int m=kb.nextInt();
		int[] moves=new int[m];
		for(int i=0; i<m; i++) moves[i]=kb.nextInt();
		System.out.println(T.solution(board, moves));
	}
}

'Coding Test Java' 카테고리의 다른 글

인프런 - 쇠막대기  (0) 2024.01.29
인프런 - 후위 연산자(postpix)  (0) 2024.01.29
인프런 - 괄호 문자 제거  (0) 2024.01.29
인프런 - 모든 아나그램 찾기  (0) 2024.01.22
인프런 - 매출액의 종류  (0) 2024.01.22