https://school.programmers.co.kr/learn/courses/30/lessons/64061
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
** 풀이 **
1. 뽑기기계 class만들기
2. 바구니 class만들기
3. 끝
import java.util.*;
class 뽑기기계{
HashMap<Integer,LinkedList<Integer>> container = new HashMap<>();
뽑기기계(int[][] board){
for(int i=0; i<board.length; i++){
var tmList = new LinkedList<Integer>();
for(int j=0; j<board.length; j++){
int doll = board[j][i];
if(doll != 0) tmList.add(doll);
}
this.container.put(i, tmList);
}
}
// 없을 시 -1 return
int pickUp(int n){
if(this.container.get(n-1).isEmpty()) return -1;
else return this.container.get(n-1).poll();
}
}
class 바구니{
LinkedList<Integer> bag = new LinkedList<>();
// 터짐 : true, 안터짐: false
boolean putIn(int doll){
if(this.bag.isEmpty()){
this.bag.add(doll);
return false;
}else{
if(this.bag.getLast() == doll){
this.bag.pollLast();
return true;
}else{
this.bag.add(doll);
return false;
}
}
}
}
class Solution {
public int solution(int[][] board, int[] moves) {
int answer = 0;
뽑기기계 mc = new 뽑기기계(board);
바구니 ba = new 바구니();
for(int i : moves) {
// 하나 뽑아서(없으면 -1)
int doll = mc.pickUp(i);
// 있으면 넣고
if(doll != -1){
// 터지면 +2
if(ba.putIn(doll)) answer+=2;
}
}
return answer;
}
}
'백준 & 프로그래머스' 카테고리의 다른 글
프로그래머스. 둘만의 암호. Java and Python (0) | 2024.01.26 |
---|---|
프로그래머스. 완주하지 못한 선수. Java and Python (0) | 2024.01.26 |
프로그래머스.교점에 별 만들기.Java and Python (0) | 2024.01.15 |
프로그래머스[2024 KAKAO WINTER INTERNSHIP]가장 많이 받은 선물.Java and Python (0) | 2024.01.10 |
뒤에 있는 큰 수 찾기 (0) | 2023.12.19 |
댓글