반응형
1. 문제 출처
2. 설계
문제 이해
- 3장의 카드가 연속 수일 경우 run
- 3장의 카드가 같은 수일 경우 triple
- run 혹은 triple로만 카드가 이뤄진 경우 baby - gin
- 우리는 baby - gin이냐 아니냐를 판별할 것이다.
CASE1. 만약 작은 수 순으로 정렬한다면?
- 012 111 이 케이스는 baby-gin이다.
- 근데, 정렬을 해버리면 011112가 되므로 baby-gin이 아니라고 뜰 것이다.
CASE2. 빈도수 배열로 정리 -> run 체크 -> triple 체크
- 0~9사이의 카드 번호를 배열의 인덱스로 빈도수 정리
numbers_count = new int[10];
// 6자리 수
for(int i=0;i<6;i++) {
numbers_count[(numbers[i]-'0')]++;
}
- run이 있는지 체크
public static boolean run() {
boolean flag = true;
for(int i=0;i<10;i++) {
if(numbers_count[i]>=1) {
if(i+1<10 && numbers_count[i+1]>=1) {
if(i+2<10 && numbers_count[i+2]>=1) {
numbers_count[i]--;
numbers_count[i+1]--;
numbers_count[i+2]--;
return true;
}
}
}
}
return false;
}
- 남은 카드로 run 혹은 triple이 되는지 체크
public static boolean triple() {
for(int i=0;i<10;i++) {
if(numbers_count[i]>=3) {
numbers_count[i]-=3;
return true;
}
}
return false;
}
- run도 안되고, triple도 안되면 false 도출
for(int T=1;T<=test;T++) {
char[] numbers = sc.next().toCharArray();
numbers_count = new int[10];
// 6자리 수
for(int i=0;i<6;i++) {
numbers_count[(numbers[i]-'0')]++;
}
if(!run()) {
if(!triple()) {
System.out.println("#"+T+" "+false);
return;
}
}else {
if(!run()) {
if(!triple()) {
System.out.println("#"+T+" "+false);
return;
}
}
}
System.out.println("#"+T+" "+true);
}//test end
3. 전체 코드
import java.util.*;
public class Solution {
static int[] numbers_count;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int test = sc.nextInt();
for(int T=1;T<=test;T++) {
char[] numbers = sc.next().toCharArray();
numbers_count = new int[10];
// 6자리 수
for(int i=0;i<6;i++) {
numbers_count[(numbers[i]-'0')]++;
}
//1. run인지 확인
if(!run()) {
if(!triple()) {
System.out.println("#"+T+" "+false);
continue;
}
}else {
if(!run()) {
if(!triple()) {
System.out.println("#"+T+" "+false);
continue;
}
}
}
System.out.println("#"+T+" "+true);
}//test end
}
public static boolean run() {
boolean flag = true;
for(int i=0;i<10;i++) {
if(numbers_count[i]>=1) {
if(i+1<10 && numbers_count[i+1]>=1) {
if(i+2<10 && numbers_count[i+2]>=1) {
numbers_count[i]--;
numbers_count[i+1]--;
numbers_count[i+2]--;
return true;
}
}
}
}
return false;
}
public static boolean triple() {
for(int i=0;i<10;i++) {
if(numbers_count[i]>=3) {
numbers_count[i]-=3;
return true;
}
}
return false;
}
}
주의점
- triple 검사 시 if(numbers_count[i]==3)으로 하면 3 이상일 때 들어가지 않으므로 if(numbers_count[i]>=3)로 해줘야 한다.
- continue 대신 return을 써주면 test case를 다 못돌고 프로그램이 종료되기 때문에 continue로 해줘야 한다.
반응형
'알고리즘 > SWEA' 카테고리의 다른 글
1767. 프로세서 연결하기 [#Permutation] (0) | 2024.01.30 |
---|---|
2001. 파리 퇴치 [#2차원 배열] (0) | 2023.10.10 |
1210. Ladder1 [#2차원 배열] (1) | 2023.10.09 |
1979. 어디에 단어가 들어갈 수 있을까 (0) | 2023.10.09 |
1208. Flatten [#1차원 배열] (0) | 2023.10.09 |