일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- session
- JWT
- hanghae99
- google oauth
- spring
- javascript
- Java
- SseEmitter
- jenkins
- oauth
- Spring Security
- 생명주기 콜백
- Stream
- cookie
- python
- web
- Project
- html
- real time web
- JPA
- DI
- Anolog
- flask
- WIL
- jQuery
- server send event
- Hibernate
- bean
- programmers
- 항해99
Archives
- Today
- Total
끄적끄적 코딩일지
[Programmers]크레인 인형뽑기(난이도:★★★★★★) 본문
https://programmers.co.kr/learn/courses/30/lessons/64061
class Solution {
public int solution(int[][] board, int[] moves) {
// return solutionA(board,moves);
return solutionB(0,0,board,moves,new int[]{});
}
// 하드 코딩
public int solutionA(int[][] board,int[] moves){
int removeCount = 0;
int moveCount = moves.length;
int[] list = {};
for(int i = 0;i < moveCount; i++){
for(int j= 0 ; j < board.length;j++){
int num = board[j][moves[i]-1];
if(num != 0){
// 꺼낸 인형이 있던 위치를 0으로 바꾼다.
board[j][moves[i]-1] = 0;
// 바구니가 비어있거나 꺼낸 인형이 바구니 마지막것과 다르다면
// 바구니에 해당 인형을 넣는다.
if(list.length == 0 || num != list[list.length-1])
list = add(list,num);
else{
// 꺼낸 인형이 바구니의 마지막것과 같다면
// 제거 숫자를 증가시키고 바구니의 마지막 값을 제거한다.
removeCount += 2;
list = remove(list);
}
break;
}
}
}
return removeCount;
}
public int[] add(int[] arry, int n){
int[] res = new int[arry.length+1];
int idx = 0;
for(int i : arry){
res[idx] = arry[idx];
idx++;
}
res[idx] = n;
return res;
}
public int[] remove(int[] arry){
int[] res= new int[arry.length-1];
for(int i = 0 ; i < arry.length-1; i++){
res[i] = arry[i];
}
return res;
}
// 제귀함수
public int solutionB(int idx,int removeCount,int[][] board,int[] moves,int[] bucket){
int dollNum = 0;
int position = moves[idx]-1;
// 인형 뽑기
for(int i = 0 ; i < board.length; i++){
if(board[i][position] != 0){
dollNum = board[i][position];
board[i][position] = 0;
break;
}
}
// 인형을 뽑으면 실행
if(dollNum != 0){
// 바구니가 비워있으면 쌓기
if(bucket.length == 0){
bucket = add(bucket,dollNum);
}
// 바구니 제일 위와 똑같으면 제거
else if(dollNum == bucket[bucket.length-1]){
removeCount+=2;
bucket = remove(bucket);
}
// 다르면 바구니에 쌓기
else{
bucket = add(bucket,dollNum);
}
}
if(idx== moves.length-1)
return removeCount;
else
return solutionB(++idx,removeCount,board,moves,bucket);
}
}
'알고리즘' 카테고리의 다른 글
[Programmers][1차] 다트게임(난이도:★★★★★★) (0) | 2022.05.17 |
---|---|
[Programmers]키페드 누르기(난이도:★★★★★★) (0) | 2022.05.17 |
[Programmers][1차] 비밀지도(난이도:★★★★★★) (0) | 2022.05.17 |
[Programmers]폰켓몬(난이도:★★★★★) (0) | 2022.05.17 |
[Programmers]체육복(난이도:★★★★★) (0) | 2022.05.17 |