programmers - 로또의 최고 순위와 최저 순위
로또 번호의 당첨 개수에 따른 로또의 최고 순위와 최저 순위를 예측
■ 문제 풀이
1) 문풀 설명
- 6개의 로또 번호와 당첨번호가 있다.
- 6개의 로또 번호 중 확인이 불가능한 번호를 0이라고 가정
- 로또 번호가 당첨 가능한 최고 순위와 최저 순위를 예측
2) 예제
순서와 상관없이, 구매한 로또에 당첨 번호와 일치하는 번호가 있으면 맞힌 걸로 인정
당첨 번호 31 10 45 1 6 19 결과 최고 순위 번호 31 0->10 44 1 0->6 25 4개 번호 일치, 3등 최저 순위 번호 31 0->11 44 1 0->7 25 2개 번호 일치, 5등
알 수 없는 두개의 번호가 당첨 번호에 속한 6개의 수 중 10과 6이라고 가정하면 최대 4개의 번호가 일치하여 3등이 된다.
알 수 없는 두개의 번호가 당첨 번호에 속하지 않은 수 11과 7이라고 가정하면, 최소 2개의 번호가 일치하여 5등이 된다.
3) 힌트
첫 번째 솔루션에서는 배열을 정렬 한 후, 로또 당첨의 최고 순위와 최저 순위를 구함
두 번째 솔루션에서는 Map을 사용하여 로또 번호가 0이 아닌 경우, Map에 담았다가 당첨번호와 일치 여부를 확인
- 최대 당첨 될 수 있는 개수 : 0의 개수 + 당첨 번호와 일치한 로또 번호의 수
- 최소 당첨 될 수 있는 개수 : 당첨 번호와 일치한 로또 번호의 수
[solution 1]
import java.util.Arrays;
public class lottos {
static int[] solution(int[] lottos, int[] win_nums) {
int[] result = new int[2];
Arrays.sort(lottos);
Arrays.sort(win_nums);
int zeroCnt = 0;
int matchCnt = 0;
for (int i = 0; i < lottos.length; i++) {
if (lottos[i] == 0)
zeroCnt++;
}
for (int i = 0 + zeroCnt; i < lottos.length; i++) {
int num = lottos[i];
for(int j=0; j<win_nums.length; j++){
if(num == win_nums[j])
matchCnt++;
}
}
result[0] = ranking(zeroCnt+matchCnt); //최고 순위
result[1] = ranking(matchCnt); //최저 순위
return result;
}
static int ranking(int totalCnt){
int rank = 0;
switch (totalCnt){
case 6:
rank = 1;
break;
case 5:
rank = 2;
break;
case 4:
rank = 3;
break;
case 3:
rank = 4;
break;
case 2:
rank = 5;
break;
default:
rank = 6;
break;
}
return rank;
}
public static void main(String[] args) {
// test case 1:
//int[] lottos = {44, 1, 0, 0, 31, 25};
//int[] win_nums = {31, 10, 45, 1, 6, 19};
// test case 2:
//int[] lottos = {0, 0, 0, 0, 0, 0};
//int[] win_nums = {38, 19, 20, 40, 15, 25};
// test case 3:
int[] lottos = {45, 4, 35, 20, 3, 9};
int[] win_nums = {20, 9, 3, 45, 4, 35};
System.out.println("answer : " + Arrays.toString(solution(lottos, win_nums)));
}
}
[solution 2]
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class lottos {
static int[] lottos(int[] lottos, int[] win_nums) {
Map<Integer, Boolean> map = new HashMap<>();
int zeroCnt = 0;
int sameCnt = 0;
for (int i = 0; i < lottos.length; i++) {
if (lottos[i] == 0) {
zeroCnt++;
continue;
}
map.put(lottos[i], true);
}
for (int i = 0; i < win_nums.length; i++) {
if (map.containsKey(win_nums[i]))
sameCnt++;
}
int maxRank = lottos.length - (zeroCnt + sameCnt) + 1;
int minRank = lottos.length - sameCnt + 1;
if (maxRank > 6) maxRank = 6;
if (minRank > 6) minRank = 6;
return new int[]{maxRank, minRank};
}
public static void main(String[] args) {
// test case 1:
//int[] lottos = {44, 1, 0, 0, 31, 25};
//int[] win_nums = {31, 10, 45, 1, 6, 19};
// test case 2:
//int[] lottos = {0, 0, 0, 0, 0, 0};
//int[] win_nums = {38, 19, 20, 40, 15, 25};
// test case 3:
int[] lottos = {45, 4, 35, 20, 3, 9};
int[] win_nums = {20, 9, 3, 45, 4, 35};
System.out.println("answer : " + Arrays.toString(lottos(lottos, win_nums)));
}
}
'CodingTest Practice' 카테고리의 다른 글
[프로그래머스] 신규 아이디 추천 (0) | 2022.03.17 |
---|---|
[programmers] 신고 결과 받기 (0) | 2022.01.18 |
[Codility 문풀] Nesting (0) | 2022.01.10 |
[Codility 문풀] Brackets (0) | 2022.01.10 |
[codingTest] anagram (0) | 2022.01.07 |