일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
29 | 30 |
Tags
- StyleSheet
- gradle
- 소스트리
- 시큐리티 로그인
- 시큐리티 로그아웃
- 프로그래머스
- 코딩테스트
- 리눅스
- Linux
- 2차원배열
- security
- springSecurity
- JAVA11
- Spring boot
- 로그인
- 스프링 부트
- 반복문
- codingtest
- 시큐리티
- springboot
- input태그
- java
- html
- css
- programmers
- 목록
- sql
- javascript
- 시큐리티로그인
- 싱글톤
Archives
- Today
- Total
JAVAIARY
프로그래머스) 크레인 인형뽑기 게임 본문
문제: https://school.programmers.co.kr/learn/courses/30/lessons/64061
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
import java.util.Stack;
class Solution {
public int solution(int[][] board, int[] moves) {
int answer = 0; // 터진 인형 개수
Stack<Integer> bucket = new Stack<>(); // 바구니
for (int i = 0; i < moves.length; i++) {
int target = moves[i]-1;
int crane = 0; // 크레인으로 뽑은 인형(숫자)
int j = 0;
while(j < board.length) {
if (board[j][target] != 0) { // 해당 칸의 인형 유무 확인
crane = board[j][target]; // 인형 뽑음
board[j][target] =0; // 인형 뽑은 칸 초기화
break;
}
j++;
}
// 해당 레인에 인형이 있었다면 실행
if (j < board.length) {
if (!bucket.empty() && crane == bucket.peek()) {
bucket.pop();
answer += 2;
} else
bucket.push(crane);
}
}
return answer;
}
}
- 스택 쓰라고 만든 문제 같다.
- 문제가 좀 헷갈리긴 했지만 재밌는 문제!
'examplePractice' 카테고리의 다른 글
프로그래머스 ) 이진 변환 반복하기 (0) | 2023.04.04 |
---|---|
프로그래머스 ) n^2 배열 자르기 (0) | 2023.04.03 |
프로그래머스) 성격유형 검사하기 (0) | 2023.03.21 |
프로그래머스) OX 퀴즈 (0) | 2023.03.21 |
프로그래머스) H-Index (0) | 2023.03.04 |